Where do you usually keep styles in rails?

This is not a blog, this is a question!
Speaking of types, I mean that we have a type value and a type description. We can have different types (gender, size, color, etc.) and a set of values โ€‹โ€‹for each type. We have 2 options: save the types in the database or in the application.
For me, saving the entire table to store type values โ€‹โ€‹for each type is redundant. Another way is to store types in one table with fields: type name, type value, type description. To keep the type in db useful if you want to change its values โ€‹โ€‹from the application. But basically, when I add a new type, I change the behavior of the application.
For me, the best choice is to save types in the application. I have a YML configuration file similar to this (this is actually part of the SettingsLogic settings file):

types:
  gender: "male female"

This way I can check the input of the form:

validates_inclusion_of :gender, :in => Settings.types.gender.split(/\s/)

Since my application is multilingual, I save the descriptions in localization files:

ru:  
  types:
    gender: 
      male: ""
      female: ""

To prepare the selection for selection, I use this helper method:

def genders
  genders = []
  Settings.types.gender.split(/\s/).each do |gender|
    genders << [t("types.gender.#{gender}"), gender]
  end
  genders
end

The disadvantage of this approach is that I have to store in db values โ€‹โ€‹of type long for men and women instead of sufficient m and f.
So I think of a hash:

def genders
  genders = []
  gender_types = { :m => "male", :f => "female" }
  gender_types.each do |key, value|
    genders << [t("types.gender.#{value}"), key]
  end
  genders
end

But at the moment I have no answer where to store the hash, as I do with the lines in the configuration file. As I said, this hash should be accessible not only from the helper method, but also when checking.

So the question is wide enough: how do you save types? What is the best or best approach?

+3
source share
1 answer

- :

class RealtyRequest < ActiveRecord::Base
   TYPOLOGY = { 'One' => 1, 'Two' => 2, 'Three' => 3 }

   def typology
     TYPOLOGY.invert[typology_id]
   end
 end

, , .

:

<div class="field">
  <%= f.label :typology_id %><br />
  <%= f.select :typology_id, RealtyRequest::TYPOLOGY %>
</div>

,

.

+3

Source: https://habr.com/ru/post/1752965/


All Articles