Class: DecentHamGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/decent_ham_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_routeObject



31
32
33
# File 'lib/generators/decent_ham_generator.rb', line 31

def add_route
  route "resources :#{name}"
end

#attach_form_fieldsObject



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_nameObject



35
36
37
# File 'lib/generators/decent_ham_generator.rb', line 35

def constant_name
  singular_name.capitalize.constantize
end

#create_controllerObject



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_modelObject



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_viewsObject



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_attributesObject



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_nameObject



8
9
10
# File 'lib/generators/decent_ham_generator.rb', line 8

def plural_name
  name
end

#singular_nameObject



4
5
6
# File 'lib/generators/decent_ham_generator.rb', line 4

def singular_name
  name.singularize
end

#strong_paramsObject



12
13
14
# File 'lib/generators/decent_ham_generator.rb', line 12

def strong_params
  strong_params = ":#{singular_name}_params"
end