Class: Somatics::Generators::RelationshipGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Extended by:
TemplatePath
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/somatics/relationship/relationship_generator.rb

Instance Method Summary collapse

Methods included from TemplatePath

source_root

Instance Method Details

#add_hidden_field_to_edit_viewObject



104
105
106
# File 'lib/generators/somatics/relationship/relationship_generator.rb', line 104

def add_hidden_field_to_edit_view
  prepend_file File.join('app/views',options[:namespace], reference_model_name.pluralize, '_form.html.erb'), "<%= f.hidden_field :#{migration_attribute} %>"
end

#add_list_view_to_model_showObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/generators/somatics/relationship/relationship_generator.rb', line 69

def add_list_view_to_model_show
  sentinel = "<!-- More List View -->\n"
  reference_list = <<-CODE
      <div class="contextual">
<%= link_to "\#{t 'Add'} \#{#{reference_class_name}.model_name.human}", '#', :class => "icon icon-add", :onclick => "showAndScrollTo('add_#{reference_model_name}','focus_#{reference_model_name}'); return false;"%>
      </div>
      <h3><%=#{reference_class_name}.model_name.human%></h3>
      <% @#{reference_value} = @#{model_name}.#{reference_value}.paginate(:page => params[:#{reference_model_name}_page], :order => (params[:#{reference_model_name}_sort].gsub('_reverse', ' DESC') unless params[:#{reference_model_name}_sort].blank?))%>
      <div class="autoscroll">
<%= render :partial => 'admin/#{reference_value}/list', :locals => {:#{reference_value} => @#{reference_value}} %>
      </div>
      <%= will_paginate @#{reference_value}, :renderer => SomaticLinkRenderer %>
      <div id="add_#{reference_model_name}" style="display:none">
<h3><%= "\#{t('New')} \#{#{reference_class_name}.model_name.human}" %></h3>
<div id="focus_#{reference_model_name}"></div>
<% form_for([:admin, @#{model_name}.#{reference_value}.build]) do |f| %>
  <%= f.error_messages %>
  <div class="issue">
    <%= render :partial => 'admin/#{reference_value}/form' , :locals => {:f => f} %>
  </div>
  <%= hidden_field_tag :return_to, url_for%>
  <%= f.submit t('Create') %>
<% end %>
<%= link_to_function t('Cancel'), "$('add_#{reference_model_name}').hide()"%>
      </div>
      CODE
  inject_into_file File.join('app/views',options[:namespace], model_name.pluralize, 'show.html.erb'), :after => sentinel do
    "#{reference_list}"
  end

  inject_into_file File.join('app/views',options[:namespace], model_name.pluralize, 'edit.html.erb'), :after => sentinel do
    "#{reference_list}"
  end
end

#add_relationship_to_modelObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/somatics/relationship/relationship_generator.rb', line 39

def add_relationship_to_model
  relation = "  has_many :#{reference_value}\n"
  # sentinel = "class #{class_name} < ActiveRecord::Base\n"
  inject_into_class "app/models/#{model_name}.rb", class_name, relation

  relation = "  belongs_to :#{model_name}\n"
  # sentinel = "class #{reference_class_name} < ActiveRecord::Base\n"
  inject_into_class "app/models/#{reference_model_name}.rb",reference_class_name, relation

end

#add_show_view_to_model_showObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/somatics/relationship/relationship_generator.rb', line 50

def add_show_view_to_model_show
  sentinel = "<!-- More List View -->\n"
  reference_list = <<-CODE
      <% if @#{reference_model_name}.#{model_name} %>
      <h3><%=link_to "\#{#{class_name}.model_name.human} #\#{@#{reference_model_name}.#{model_name}.id}", [:admin, @#{reference_model_name}.#{model_name}] %></h3>
      <div class="issue detail">
      	<%= render :partial => 'admin/#{model_name.pluralize}/show' , :locals => {:#{model_name} => @#{reference_model_name}.#{model_name}} %>
      </div>
      <% end %>
  CODE
  inject_into_file File.join('app/views',options[:namespace], reference_model_name.pluralize, 'show.html.erb'), :after => sentinel do 
    "#{reference_list}"
  end
  
  inject_into_file File.join('app/views',options[:namespace], reference_model_name.pluralize, 'edit.html.erb'), :after => sentinel do
    "#{reference_list}"
  end
end

#dump_generator_attribute_namesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/somatics/relationship/relationship_generator.rb', line 14

def dump_generator_attribute_names
  generator_attribute_names = [
    :name,
    :singular_name,
    :human_name,
    :plural_name,
    :relationship,
    :model_name,
    :class_name,
    :reference_model_name,
    :reference_class_name,
    :reference_value, 
    :migration_model_name,
    :migration_table_name,
    :migration_attribute ,
    :table_name

  ]
       
  generator_attribute_names.each do |attr|
    puts "%-40s %s" % ["#{attr}:", self.send(attr.to_s)]  # instance_variable_get("@#{attr.to_s}"
  end
       
end

#migrationsObject



108
109
110
111
112
113
114
# File 'lib/generators/somatics/relationship/relationship_generator.rb', line 108

def migrations
  if relationship == 'has_many'
    unless options[:skip_migration] 
      invoke "migration", [%(add_#{migration_attribute}_to_#{migration_table_name}), "#{migration_attribute}:integer"]
    end
  end
end