Class: DecentHamGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- DecentHamGenerator
- Defined in:
- lib/generators/decent_ham_generator.rb
Instance Method Summary collapse
- #add_route ⇒ Object
- #attach_form_fields ⇒ Object
- #constant_name ⇒ Object
- #create_controller ⇒ Object
- #create_model ⇒ Object
- #create_views ⇒ Object
- #model_attributes ⇒ Object
- #plural_name ⇒ Object
- #singular_name ⇒ Object
- #strong_params ⇒ Object
Instance Method Details
#add_route ⇒ Object
31 32 33 |
# File 'lib/generators/decent_ham_generator.rb', line 31 def add_route route "resources :#{name}" end |
#attach_form_fields ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/generators/decent_ham_generator.rb', line 39 def attach_form_fields model_attributes.each do |attribute| attribute.form_field = case attribute.data_type when :string || :decimal || :integer || :float || :references "f.text_field :#{attribute.name}" when :text "f.text_area :#{attribute.name}" when :datetime || :timestamp "f.datetime_select :#{attribute.name}" when :boolean "f.check_box :#{attribute.name}" when :date "f.date_select :#{attribute.name}" when :time "f.time_select :#{attribute.name}" end end end |
#constant_name ⇒ Object
35 36 37 |
# File 'lib/generators/decent_ham_generator.rb', line 35 def constant_name singular_name.capitalize.constantize end |
#create_controller ⇒ Object
26 27 28 29 |
# File 'lib/generators/decent_ham_generator.rb', line 26 def create_controller @attributes = model_attributes template "controller.rb", "app/controllers/#{name}_controller.rb" end |
#create_model ⇒ Object
16 17 18 |
# File 'lib/generators/decent_ham_generator.rb', line 16 def create_model template "model.rb", "app/models/#{singular_name}.rb" end |
#create_views ⇒ Object
59 60 61 62 63 64 |
# File 'lib/generators/decent_ham_generator.rb', line 59 def create_views @attributes = attach_form_fields %w(index new edit _form show).each do |view_name| template "#{view_name}.html.haml", "app/views/#{name}/#{view_name}.html.haml" end end |
#model_attributes ⇒ Object
20 21 22 23 24 |
# File 'lib/generators/decent_ham_generator.rb', line 20 def model_attributes constant_name.columns_hash.map do |key, value| OpenStruct.new(name: key, data_type: value.type) end.delete_if { |column| %w(id created_at updated_at).include? column.name } end |
#plural_name ⇒ Object
8 9 10 |
# File 'lib/generators/decent_ham_generator.rb', line 8 def plural_name name end |
#singular_name ⇒ Object
4 5 6 |
# File 'lib/generators/decent_ham_generator.rb', line 4 def singular_name name.singularize end |
#strong_params ⇒ Object
12 13 14 |
# File 'lib/generators/decent_ham_generator.rb', line 12 def strong_params strong_params = ":#{singular_name}_params" end |