Class: Somatics::Generators::AssociatedGenerator

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

Instance Method Summary collapse

Methods included from TemplatePath

source_root

Instance Method Details

#add_association_to_modelObject



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_localesObject



100
101
102
103
104
105
106
# File 'lib/generators/somatics/associated/associated_generator.rb', line 100

def add_attribute_to_locales
  options[: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_namesObject



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_scaffoldObject



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 => options[:namespace], :header => false }                    
end

#migrate_attributesObject



129
130
131
132
133
# File 'lib/generators/somatics/associated/associated_generator.rb', line 129

def migrate_attributes
  unless options[:skip_migration]
    invoke "migration", [%(add_#{associated_singular_name}_to_#{table_name}), "#{associated_singular_name}_id:integer"]
  end
end

#update_form_partialObject



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/',options.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_partialObject



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 &quot;#{attribute.name.humanize}&quot;\"><%= 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_menuObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/generators/somatics/associated/associated_generator.rb', line 62

def update_menu
  # add attributes type to submenu
  # look_for = /<li ><%= link_to 'Product', '\/admin\/products', :class => (match_controller?('products'))  \? 'selected' : ''%>.*<ul>/
  look_for = /<%=.*\/#{options.namespace}\/#{plural_name}.*%>[^<]*<ul[^>]*>/
  inject_into_file File.join('app/views/',options.namespace, 'shared/_menu.html.erb'), :after => look_for do 
    <<-RUBY
    
      <li><%=link_to #{class_name}.human_attribute_name(:#{associated_name}), '/#{options.namespace}/#{associated_plural_name}' %></li>
    RUBY
  end
  
end

#update_show_partialObject



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/',options.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