Class: DecentHam::DecentHamGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- DecentHam::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
34 35 36 |
# File 'lib/generators/decent_ham_generator.rb', line 34 def add_route route "resources :#{name}" end |
#attach_form_fields ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/generators/decent_ham_generator.rb', line 42 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
38 39 40 |
# File 'lib/generators/decent_ham_generator.rb', line 38 def constant_name singular_name.capitalize.constantize end |
#create_controller ⇒ Object
29 30 31 32 |
# File 'lib/generators/decent_ham_generator.rb', line 29 def create_controller @attributes = model_attributes template "controller.rb", "app/controllers/#{name}_controller.rb" end |
#create_model ⇒ Object
19 20 21 |
# File 'lib/generators/decent_ham_generator.rb', line 19 def create_model template "model.rb", "app/models/#{singular_name}.rb" end |
#create_views ⇒ Object
62 63 64 65 66 67 |
# File 'lib/generators/decent_ham_generator.rb', line 62 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
23 24 25 26 27 |
# File 'lib/generators/decent_ham_generator.rb', line 23 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
11 12 13 |
# File 'lib/generators/decent_ham_generator.rb', line 11 def plural_name name end |
#singular_name ⇒ Object
7 8 9 |
# File 'lib/generators/decent_ham_generator.rb', line 7 def singular_name name.singularize end |
#strong_params ⇒ Object
15 16 17 |
# File 'lib/generators/decent_ham_generator.rb', line 15 def strong_params strong_params = ":#{singular_name}_params" end |