Class: Somatics::Generators::AssociatedGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- Somatics::Generators::AssociatedGenerator
- Extended by:
- TemplatePath
- Includes:
- Rails::Generators::Migration, Rails::Generators::ResourceHelpers
- Defined in:
- lib/generators/somatics/associated/associated_generator.rb
Instance Method Summary collapse
- #add_association_to_model ⇒ Object
- #add_attribute_to_locales ⇒ Object
- #dump_generator_attribute_names ⇒ Object
- #generate_associated_scaffold ⇒ Object
- #migrate_attributes ⇒ Object
- #update_form_partial ⇒ Object
- #update_list_partial ⇒ Object
- #update_menu ⇒ Object
- #update_show_partial ⇒ Object
Methods included from TemplatePath
Instance Method Details
#add_association_to_model ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 108 def add_association_to_model inject_into_class "app/models/#{name}.rb", class_name do <<-RUBY belongs_to :#{associated_name} def #{associated_name}_#{attribute_name} self.#{associated_name}.#{attribute_name} unless self.#{associated_name}.blank? end def #{associated_name}_#{attribute_name}=(#{associated_name}_#{attribute_name}) self.#{associated_name} = #{associated_class_name}.find_by_#{attribute_name}(#{associated_name}_#{attribute_name}) || #{associated_class_name}.new(:#{attribute_name} => #{associated_name}_#{attribute_name}) end RUBY end inject_into_class "app/models/#{associated_name}.rb", associated_class_name do <<-RUBY has_many :#{class_name} RUBY end rescue nil end |
#add_attribute_to_locales ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 100 def add_attribute_to_locales [:locales].each do |locale| append_file File.join('config/locales', "#{controller_file_name}_#{locale}.yml") do " #{associated_name}: #{GoogleTranslate.t associated_human_name, locale}\n" end end end |
#dump_generator_attribute_names ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 32 def dump_generator_attribute_names generator_attribute_names = [ :name, :singular_name, :human_name, :plural_name, :table_name, :class_name, :controller_class_path, :controller_file_name, :attribute_name, :attribute_type, :associated_name, :associated_singular_name, :associated_human_name, :associated_plural_name, :associated_table_name, :associated_class_name, ] generator_attribute_names.each do |attr| # puts "%-40s %s" % ["#{attr}:", self.send(attr.to_s)] # instance_variable_get("@#{attr.to_s}" end end |
#generate_associated_scaffold ⇒ Object
58 59 60 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 58 def generate_associated_scaffold invoke 'somatics:scaffold', [associated_class_name, "#{attribute_name}:#{attribute_type}"], {:namespace => [:namespace], :header => false } end |
#migrate_attributes ⇒ Object
129 130 131 132 133 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 129 def migrate_attributes unless [:skip_migration] invoke "migration", [%(add_#{associated_singular_name}_to_#{table_name}), "#{associated_singular_name}_id:integer"] end end |
#update_form_partial ⇒ Object
82 83 84 85 86 87 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 82 def update_form_partial look_for = "</tbody>\n</table>" inject_into_file File.join('app/views/',.namespace, controller_class_path, controller_file_name, '_form.html.erb'), :before => look_for do " <tr>\n <td><b><%= #{class_name}.human_attribute_name(:#{associated_name}) %></b></td>\n <td><%= f.collection_select :#{associated_name}_id, #{associated_class_name}.all, :id, :#{attribute_name}, :prompt => true %></td>\n </tr>\n" end end |
#update_list_partial ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 89 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 # " <th title=\"Sort by "#{attribute.name.humanize}"\"><%= sort_link_helper #{class_name}.human_attribute_name(:#{attribute.name}), '#{singular_name}', '#{attribute.name}' %></th>\n" # 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 # " <td onclick=\"link_to(<%= \"'\#{admin_#{singular_name}_path(#{singular_name})}'\" %>);\" class=\"#{attribute.name}\"><%=h #{singular_name}.#{attribute.name} %></td>\n" # end end |
#update_menu ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 62 def # add attributes type to submenu # look_for = /<li ><%= link_to 'Product', '\/admin\/products', :class => (match_controller?('products')) \? 'selected' : ''%>.*<ul>/ look_for = /<%=.*\/#{.namespace}\/#{plural_name}.*%>[^<]*<ul[^>]*>/ inject_into_file File.join('app/views/',.namespace, 'shared/_menu.html.erb'), :after => look_for do <<-RUBY <li><%=link_to #{class_name}.human_attribute_name(:#{associated_name}), '/#{.namespace}/#{associated_plural_name}' %></li> RUBY end end |
#update_show_partial ⇒ Object
75 76 77 78 79 80 |
# File 'lib/generators/somatics/associated/associated_generator.rb', line 75 def update_show_partial look_for = "</tbody>\n</table>" inject_into_file File.join('app/views/',.namespace, controller_class_path, controller_file_name, '_show.html.erb'), :before => look_for do " <tr>\n <td><b><%= #{class_name}.human_attribute_name(:#{associated_name}) %></b></td>\n <td><%=h #{singular_name}.#{associated_name}_#{attribute_name}%></td>\n </tr>\n" end end |