Class: DecentHam::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



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

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

#attach_form_fieldsObject



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_nameObject



38
39
40
# File 'lib/generators/decent_ham_generator.rb', line 38

def constant_name
  singular_name.capitalize.constantize
end

#create_controllerObject



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_modelObject



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_viewsObject



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_attributesObject



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_nameObject



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

def plural_name
  name
end

#singular_nameObject



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

def singular_name
  name.singularize
end

#strong_paramsObject



15
16
17
# File 'lib/generators/decent_ham_generator.rb', line 15

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