Class: Somatics::Generators::AttributesGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Extended by:
TemplatePath
Includes:
Rails::Generators::Migration, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/somatics/attributes/attributes_generator.rb

Instance Method Summary collapse

Methods included from TemplatePath

source_root

Instance Method Details

#dump_generator_attribute_namesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/somatics/attributes/attributes_generator.rb', line 30

def dump_generator_attribute_names
  generator_attribute_names = [
    :name,
    :singular_name,
    :human_name,
    :plural_name,
    :table_name,
    :attributes,
    :class_name,
    :controller_class_path,
    :controller_file_name,
  ]
       
  generator_attribute_names.each do |attr|
    # puts "%-40s %s" % ["#{attr}:", self.send(attr.to_s)]  # instance_variable_get("@#{attr.to_s}"
  end
       
end

#migrate_attributesObject



91
92
93
94
95
96
# File 'lib/generators/somatics/attributes/attributes_generator.rb', line 91

def migrate_attributes
  unless options[:skip_migration]
    migration_name = attributes.collect(&:name).join('_and_')
    invoke "migration", [%(add_#{migration_name}_to_#{table_name}), attributes.collect{|a| "#{a.name}:#{a.type}"}]
  end
end

#update_form_partialObject



57
58
59
60
61
62
63
64
# File 'lib/generators/somatics/attributes/attributes_generator.rb', line 57

def update_form_partial
  look_for = "</tbody>\n</table>"
  inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_form.html.erb'), :before => look_for do 
    attributes.inject('') do |str, attribute|
      "  <tr>\n    <td><b><%= #{class_name}.human_attribute_name(:#{attribute.name}) %></b></td>\n    <td><%= f.#{attribute.field_type} :#{attribute.name} %></td>\n  </tr>\n"
    end
  end
end

#update_list_partialObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/generators/somatics/attributes/attributes_generator.rb', line 66

def update_list_partial
  look_for = "      <!-- More Sort Link Helper -->"
  inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_list.html.erb'), :after => look_for do
    attributes.inject('') do |str, attribute|
      str + "      <th title=\"Sort by &quot;#{attribute.name.humanize}&quot;\"><%= sort_link_helper #{class_name}.human_attribute_name(:#{attribute.name}), '#{singular_name}', '#{attribute.name}' %></th>\n"
    end
  end
  look_for = "      <!-- More Fields -->"
  inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_list.html.erb'), :after => look_for do 
    result = attributes.inject('') do |str, attribute|
      str + "      <td onclick=\"link_to(<%= \"'\#{admin_#{singular_name}_path(#{singular_name})}'\" %>);\" class=\"#{attribute.name}\"><%=h #{singular_name}.#{attribute.name} %></td>\n"
    end
  end
end

#update_localesObject



81
82
83
84
85
86
87
88
89
# File 'lib/generators/somatics/attributes/attributes_generator.rb', line 81

def update_locales
  options[:locales].each do |locale|
    append_file File.join('config/locales', "#{controller_file_name}_#{locale}.yml") do
      attributes.inject('') do |str, attribute|
        "        #{attribute.name}: #{GoogleTranslate.t attribute.name.humanize, locale}\n"
      end
    end
  end
end

#update_show_partialObject



48
49
50
51
52
53
54
55
# File 'lib/generators/somatics/attributes/attributes_generator.rb', line 48

def update_show_partial
  look_for = "</tbody>\n</table>"
  inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_show.html.erb'), :before => look_for do
    attributes.inject('') do |str, attribute|
      "  <tr>\n    <td><b><%= #{class_name}.human_attribute_name(:#{attribute.name}) %></b></td>\n    <td><%=h #{singular_name}.#{attribute.name} %></td>\n  </tr>\n"
    end
  end
end