somatics3-generators 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -2,12 +2,27 @@ require 'generators/somatics'
2
2
  require 'rails/generators/named_base'
3
3
  require 'rails/generators/migration'
4
4
 
5
+ require 'net/http'
6
+ require 'uri'
7
+ require 'json'
8
+ class GoogleTranslate
9
+ def self.t(text)
10
+ uri = URI.parse('http://ajax.googleapis.com/ajax/services/language/translate')
11
+ JSON.parse(Net::HTTP.post_form(uri, {"q" => text, "langpair" => "en|zh-TW", "v" => '1.0'}).body)['responseData']['translatedText'] rescue text.humanize
12
+ end
13
+ end
14
+
15
+
5
16
  module Somatics
6
17
  module Generators
7
18
  class AttributesGenerator < Rails::Generators::NamedBase
8
19
  extend TemplatePath
9
20
  include Rails::Generators::Migration
10
- # include Rails::Generators::ResourceHelpers
21
+ include Rails::Generators::ResourceHelpers
22
+
23
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
24
+ class_option :namespace, :banner => "NAME", :type => :string, :default => 'admin'
25
+ class_option :skip_migration, :type => :boolean, :desc => "Don't generate a migration file for this model."
11
26
 
12
27
  def dump_generator_attribute_names
13
28
  generator_attribute_names = [
@@ -15,17 +30,11 @@ module Somatics
15
30
  :singular_name,
16
31
  :human_name,
17
32
  :plural_name,
18
- :relationship,
19
- :model_name,
33
+ :table_name,
34
+ :attributes,
20
35
  :class_name,
21
- :reference_model_name,
22
- :reference_class_name,
23
- :reference_value,
24
- :migration_model_name,
25
- :migration_table_name,
26
- :migration_attribute ,
27
- :table_name
28
-
36
+ :controller_class_path,
37
+ :controller_file_name,
29
38
  ]
30
39
 
31
40
  generator_attribute_names.each do |attr|
@@ -33,131 +42,58 @@ module Somatics
33
42
  end
34
43
 
35
44
  end
36
-
37
- def add_relationship_to_model
38
- relation = "has_many :#{reference_value}"
39
- sentinel = "class #{class_name} < ActiveRecord::Base\n"
40
- gsub_file File.join('app/models', "#{model_name}.rb"), /(#{Regexp.escape(sentinel)})/mi do |match|
41
- "#{match} #{relation}\n"
45
+ def update_show_partial
46
+ look_for = "</tbody>\n</table>"
47
+ inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_show.html.erb'), :before => look_for do
48
+ attributes.inject('') do |str, attribute|
49
+ " <tr>\n <td><b><%= #{class_name}.human_attribute_name(:#{attribute.name}) %></b></td>\n <td><%=h #{singular_name}.#{attribute.name} %></td>\n </tr>\n"
50
+ end
42
51
  end
43
- # logger.insert relation
44
- relation = "belongs_to :#{model_name}"
45
- sentinel = "class #{reference_class_name} < ActiveRecord::Base\n"
46
- gsub_file File.join('app/models', "#{reference_model_name}.rb"), /(#{Regexp.escape(sentinel)})/mi do |match|
47
- "#{match} #{relation}\n"
52
+ end
53
+
54
+ def update_form_partial
55
+ look_for = "</tbody>\n</table>"
56
+ inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_form.html.erb'), :before => look_for do
57
+ attributes.inject('') do |str, attribute|
58
+ " <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"
59
+ end
48
60
  end
49
- # logger.insert relation
50
61
  end
51
62
 
52
- def add_show_view_to_model_show
53
- sentinel = "<!-- More List View -->\n"
54
- reference_list = <<-CODE
55
- <% if @#{reference_model_name}.#{model_name} %>
56
- <h3><%=link_to "\#{#{class_name}.human_name} #\#{@#{reference_model_name}.#{model_name}.id}", [:admin, @#{reference_model_name}.#{model_name}] %></h3>
57
- <div class="issue detail">
58
- <%= render :partial => 'admin/#{model_name.pluralize}/show' , :locals => {:#{model_name} => @#{reference_model_name}.#{model_name}} %>
59
- </div>
60
- <% end %>
61
- CODE
62
- gsub_file File.join('app/views/admin', reference_model_name.pluralize, 'show.html.erb'), /(#{Regexp.escape(sentinel)})/mi do |match|
63
- "#{match}#{reference_list}"
64
- end
65
- # logger.update File.join('app/views/admin', model_name.pluralize, 'show.html.erb')
66
- gsub_file File.join('app/views/admin', reference_model_name.pluralize, 'edit.html.erb'), /(#{Regexp.escape(sentinel)})/mi do |match|
67
- "#{match}#{reference_list}"
68
- end
69
- # logger.update File.join('app/views/admin', reference_model_name.pluralize, 'edit.html.erb')
70
- end
71
-
72
- def add_list_view_to_model_show
73
- sentinel = "<!-- More List View -->\n"
74
- reference_list = <<-CODE
75
- <div class="contextual">
76
- <%= link_to "\#{t 'Add'} \#{#{reference_class_name}.human_name}", '#', :class => "icon icon-add", :onclick => "showAndScrollTo('add_#{reference_model_name}','focus_#{reference_model_name}'); return false;"%>
77
- </div>
78
- <h3><%=#{reference_class_name}.human_name%></h3>
79
- <% @#{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?))%>
80
- <div class="autoscroll">
81
- <%= render :partial => 'admin/#{reference_value}/list', :locals => {:#{reference_value} => @#{reference_value}} %>
82
- </div>
83
- <%= will_paginate @#{reference_value}, :renderer => SomaticLinkRenderer %>
84
- <div id="add_#{reference_model_name}" style="display:none">
85
- <h3><%= "\#{t('New')} \#{#{reference_class_name}.human_name}" %></h3>
86
- <div id="focus_#{reference_model_name}"></div>
87
- <% form_for([:admin, @#{model_name}.#{reference_value}.build]) do |f| %>
88
- <%= f.error_messages %>
89
- <div class="issue">
90
- <%= render :partial => 'admin/#{reference_value}/form' , :locals => {:f => f} %>
91
- </div>
92
- <%= hidden_field_tag :return_to, url_for%>
93
- <%= f.submit t('Create') %>
94
- <% end %>
95
- <%= link_to_function t('Cancel'), "$('add_#{reference_model_name}').hide()"%>
96
- </div>
97
- CODE
98
- gsub_file File.join('app/views/admin', model_name.pluralize, 'show.html.erb'), /(#{Regexp.escape(sentinel)})/mi do |match|
99
- "#{match}#{reference_list}"
63
+ def update_list_partial
64
+ look_for = " <!-- More Sort Link Helper -->"
65
+ inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_list.html.erb'), :after => look_for do
66
+ attributes.inject('') do |str, attribute|
67
+ 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"
68
+ end
100
69
  end
101
- # logger.update File.join('app/views/admin', model_name.pluralize, 'show.html.erb')
102
- gsub_file File.join('app/views/admin', model_name.pluralize, 'edit.html.erb'), /(#{Regexp.escape(sentinel)})/mi do |match|
103
- "#{match}#{reference_list}"
70
+ look_for = " <!-- More Fields -->"
71
+ inject_into_file File.join('app/views/',options.namespace, controller_class_path, controller_file_name, '_list.html.erb'), :after => look_for do
72
+ result = attributes.inject('') do |str, attribute|
73
+ str + " <td onclick=\"link_to(<%= \"'\#{admin_#{singular_name}_path(#{singular_name})}'\" %>);\" class=\"#{attribute.name}\"><%=h #{singular_name}.#{attribute.name} %></td>\n"
74
+ end
104
75
  end
105
- # logger.update File.join('app/views/admin', model_name.pluralize, 'edit.html.erb')
106
76
  end
107
77
 
108
- def migrations
109
- if relationship == 'has_many'
110
-
111
- #TODO : dependency 'admin_attributes', [migration_model_name, "#{migration_attribute}:integer"], :skip_migration => true unless options[:skip_views]
112
- # raise 'migration_exists' if m.migration_exists?("add_#{migration_attribute}_to_#{migration_table_name}")
113
- unless options[:skip_migration]
114
- migration_template 'migration.rb', "db/migrate/add_#{migration_attribute}_to_#{migration_table_name}"
78
+ def update_locales
79
+ %w( en zh-TW ).each do |locale|
80
+ append_file File.join('config/locales', "#{controller_file_name}_#{locale}.yml") do
81
+ attributes.inject('') do |str, attribute|
82
+ " #{attribute.name}: #{GoogleTranslate.t attribute.name.humanize}\n"
83
+ end
115
84
  end
116
85
  end
117
86
  end
118
-
119
-
120
- protected
121
-
122
- def match_data
123
- @match_data ||= name.match(/(.*)_(has_many|belongs_to)_(.*)/)
124
- end
125
87
 
126
- def relationship
127
- match_data[2]
128
- end
129
-
130
- def model_name
131
- match_data[1].singularize
132
- end
133
-
134
- def class_name
135
- model_name.classify
136
- end
137
-
138
- def reference_model_name
139
- match_data[3].singularize
140
- end
141
-
142
- def reference_class_name
143
- reference_model_name.classify
144
- end
145
-
146
- def reference_value
147
- reference_model_name.pluralize
88
+ def migrate_attributes
89
+ unless options[:skip_migration]
90
+ migration_name = attributes.collect(&:name).join('_and_')
91
+ invoke "migration", [%(add_#{migration_name}_to_#{table_name}), attributes.collect{|a| "#{a.name}:#{a.type}"}]
92
+ end
148
93
  end
149
94
 
150
- def migration_table_name
151
- reference_value
152
- end
153
95
 
154
- def migration_model_name
155
- reference_model_name
156
- end
157
-
158
- def migration_attribute
159
- "#{model_name}_id"
160
- end
96
+ protected
161
97
 
162
98
  #
163
99
  # Implement the required interface for Rails::Generators::Migration.
@@ -171,27 +107,6 @@ module Somatics
171
107
  end
172
108
  end
173
109
 
174
- def legacy_functions
175
- raise banner unless match_data = @command.match(/(.*)_(has_many|belongs_to)_(.*)/)
176
- @relationship = match_data[2]
177
- @model_name = match_data[1].singularize
178
- @class_name = @model_name.camelize
179
- @reference_model_name = match_data[3].singularize
180
- @reference_class_name = @reference_model_name.camelize
181
- case @relationship
182
- when 'has_many'
183
- @reference_value = @reference_model_name.pluralize
184
- @migration_model_name = @reference_model_name
185
- @migration_table_name = @reference_value
186
- @migration_attribute = "#{@model_name}_id"
187
- when 'belongs_to'
188
- # @reference_value = @reference_model_name
189
- # @migration_model_name = @model_name
190
- # @migration_table_name = @model_name.pluralize
191
- # @migration_attribute = "#{@reference_model_name}_id"
192
- end
193
- end
194
-
195
110
  end
196
111
  end
197
112
  end
@@ -37,11 +37,11 @@ module Somatics
37
37
  end
38
38
 
39
39
  def add_relationship_to_model
40
- relation = " has_many :#{reference_value}"
40
+ relation = " has_many :#{reference_value}\n"
41
41
  # sentinel = "class #{class_name} < ActiveRecord::Base\n"
42
42
  inject_into_class "app/models/#{model_name}.rb", class_name, relation
43
43
 
44
- relation = " belongs_to :#{model_name}"
44
+ relation = " belongs_to :#{model_name}\n"
45
45
  # sentinel = "class #{reference_class_name} < ActiveRecord::Base\n"
46
46
  inject_into_class "app/models/#{reference_model_name}.rb",reference_class_name, relation
47
47
 
@@ -51,7 +51,7 @@ module Somatics
51
51
  sentinel = "<!-- More List View -->\n"
52
52
  reference_list = <<-CODE
53
53
  <% if @#{reference_model_name}.#{model_name} %>
54
- <h3><%=link_to "\#{#{class_name}.human_name} #\#{@#{reference_model_name}.#{model_name}.id}", [:admin, @#{reference_model_name}.#{model_name}] %></h3>
54
+ <h3><%=link_to "\#{#{class_name}.model_name.human} #\#{@#{reference_model_name}.#{model_name}.id}", [:admin, @#{reference_model_name}.#{model_name}] %></h3>
55
55
  <div class="issue detail">
56
56
  <%= render :partial => 'admin/#{model_name.pluralize}/show' , :locals => {:#{model_name} => @#{reference_model_name}.#{model_name}} %>
57
57
  </div>
@@ -70,16 +70,16 @@ module Somatics
70
70
  sentinel = "<!-- More List View -->\n"
71
71
  reference_list = <<-CODE
72
72
  <div class="contextual">
73
- <%= link_to "\#{t 'Add'} \#{#{reference_class_name}.human_name}", '#', :class => "icon icon-add", :onclick => "showAndScrollTo('add_#{reference_model_name}','focus_#{reference_model_name}'); return false;"%>
73
+ <%= 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;"%>
74
74
  </div>
75
- <h3><%=#{reference_class_name}.human_name%></h3>
75
+ <h3><%=#{reference_class_name}.model_name.human%></h3>
76
76
  <% @#{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?))%>
77
77
  <div class="autoscroll">
78
78
  <%= render :partial => 'admin/#{reference_value}/list', :locals => {:#{reference_value} => @#{reference_value}} %>
79
79
  </div>
80
80
  <%= will_paginate @#{reference_value}, :renderer => SomaticLinkRenderer %>
81
81
  <div id="add_#{reference_model_name}" style="display:none">
82
- <h3><%= "\#{t('New')} \#{#{reference_class_name}.human_name}" %></h3>
82
+ <h3><%= "\#{t('New')} \#{#{reference_class_name}.model_name.human}" %></h3>
83
83
  <div id="focus_#{reference_model_name}"></div>
84
84
  <% form_for([:admin, @#{model_name}.#{reference_value}.build]) do |f| %>
85
85
  <%= f.error_messages %>
@@ -99,7 +99,6 @@ module Somatics
99
99
  inject_into_file File.join('app/views',options[:namespace], model_name.pluralize, 'edit.html.erb'), :after => sentinel do
100
100
  "#{reference_list}"
101
101
  end
102
- # logger.update File.join('app/views/admin', model_name.pluralize, 'edit.html.erb')
103
102
  end
104
103
 
105
104
  def add_hidden_field_to_edit_view
@@ -108,11 +107,8 @@ module Somatics
108
107
 
109
108
  def migrations
110
109
  if relationship == 'has_many'
111
-
112
- #TODO : dependency 'admin_attributes', [migration_model_name, "#{migration_attribute}:integer"], :skip_migration => true unless options[:skip_views]
113
- # raise 'migration_exists' if m.migration_exists?("add_#{migration_attribute}_to_#{migration_table_name}")
114
110
  unless options[:skip_migration]
115
- migration_template 'migration.rb', "db/migrate/add_#{migration_attribute}_to_#{migration_table_name}"
111
+ invoke "migration", [%(add_#{migration_attribute}_to_#{migration_table_name}), "#{migration_attribute}:integer"]
116
112
  end
117
113
  end
118
114
  end
@@ -172,27 +168,6 @@ module Somatics
172
168
  end
173
169
  end
174
170
 
175
- def legacy_functions
176
- raise banner unless match_data = @command.match(/(.*)_(has_many|belongs_to)_(.*)/)
177
- @relationship = match_data[2]
178
- @model_name = match_data[1].singularize
179
- @class_name = @model_name.camelize
180
- @reference_model_name = match_data[3].singularize
181
- @reference_class_name = @reference_model_name.camelize
182
- case @relationship
183
- when 'has_many'
184
- @reference_value = @reference_model_name.pluralize
185
- @migration_model_name = @reference_model_name
186
- @migration_table_name = @reference_value
187
- @migration_attribute = "#{@model_name}_id"
188
- when 'belongs_to'
189
- # @reference_value = @reference_model_name
190
- # @migration_model_name = @model_name
191
- # @migration_table_name = @model_name.pluralize
192
- # @migration_attribute = "#{@reference_model_name}_id"
193
- end
194
- end
195
-
196
171
  end
197
172
  end
198
173
  end
@@ -1,7 +1,7 @@
1
1
  <table width="100%">
2
2
  <tbody>
3
3
  <tr>
4
- <td colspan="2"><br/><h3><%%= <%=class_name %>.human_name %></h3></td>
4
+ <td colspan="2"><br/><h3><%%= <%=class_name %>.model_name.human %></h3></td>
5
5
  </tr>
6
6
  <% for attribute in attributes -%>
7
7
  <tr>
@@ -1,7 +1,7 @@
1
1
  <table class="attributes" width="100%">
2
2
  <tbody>
3
3
  <tr>
4
- <td colspan="2"><br/><h3><%%= "#{<%= class_name %>.human_name} #{t('Information')}" %></h3></td>
4
+ <td colspan="2"><br/><h3><%%= "#{<%= class_name %>.model_name.human} #{t('Information')}" %></h3></td>
5
5
  </tr>
6
6
  <% if options[:admin_authenticated] || options[:authenticated] -%>
7
7
  <tr>
@@ -3,7 +3,7 @@
3
3
  <%%= link_to t('Delete'), [:admin,@<%= singular_name %>], :confirm => 'Are you sure?', :method => :delete ,:class => 'icon icon-del' %>
4
4
  </div>
5
5
 
6
- <h2><%%= "#{<%= class_name %>.human_name} ##{@<%= singular_name %>.id}" %></h2>
6
+ <h2><%%= "#{<%= class_name %>.model_name.human} ##{@<%= singular_name %>.id}" %></h2>
7
7
 
8
8
  <div class="issue details">
9
9
  <%%= render :partial => 'show' , :locals => {:<%= singular_name %> => @<%= singular_name %>} %>
@@ -20,5 +20,5 @@
20
20
 
21
21
  <%% content_for :sidebar do %>
22
22
  <h3>Actions</h3>
23
- <%%= link_to "#{t('Back_to')} #{<%= class_name %>.human_name}", admin_<%= plural_name %>_path %>
23
+ <%%= link_to "#{t('Back_to')} #{<%= class_name %>.model_name.human}", admin_<%= plural_name %>_path %>
24
24
  <%% end %>
@@ -1,6 +1,6 @@
1
1
  <div class="contextual"></div>
2
2
 
3
- <h2><%%= <%= class_name %>.human_name %></h2>
3
+ <h2><%%= <%= class_name %>.model_name.human %></h2>
4
4
 
5
5
  <!-- Redmine Filters-->
6
6
  <%%#= filters %>
@@ -24,5 +24,5 @@
24
24
 
25
25
  <%% content_for :sidebar do %>
26
26
  <h3>Actions</h3>
27
- <%%= link_to "#{t('new')} #{<%= class_name %>.human_name}", new_admin_<%= singular_name %>_path %>
27
+ <%%= link_to "#{t('new')} #{<%= class_name %>.model_name.human}", new_admin_<%= singular_name %>_path %>
28
28
  <%% end %>
@@ -1,4 +1,4 @@
1
- <h2><%%= "#{t('New')} #{<%= class_name %>.human_name}" %></h2>
1
+ <h2><%%= "#{t('New')} #{<%= class_name %>.model_name.human}" %></h2>
2
2
  <%% form_for([:admin, @<%= singular_name %>]) do |f| %>
3
3
  <%%= f.error_messages %>
4
4
  <div class="issue">
@@ -10,5 +10,5 @@
10
10
 
11
11
  <%% content_for :sidebar do %>
12
12
  <h3>Actions</h3>
13
- <%%= link_to "#{t('Back_to')} #{<%= class_name %>.human_name}", admin_<%= plural_name %>_path %>
13
+ <%%= link_to "#{t('Back_to')} #{<%= class_name %>.model_name.human}", admin_<%= plural_name %>_path %>
14
14
  <%% end %>
@@ -3,7 +3,7 @@
3
3
  <%%= link_to t('Delete'), [:admin,@<%= singular_name %>], :confirm => 'Are you sure?', :method => :delete ,:class => 'icon icon-del' %>
4
4
  </div>
5
5
 
6
- <h2><%%= "#{<%= class_name %>.human_name} ##{@<%= singular_name %>.id}" %></h2>
6
+ <h2><%%= "#{<%= class_name %>.model_name.human} ##{@<%= singular_name %>.id}" %></h2>
7
7
 
8
8
  <div class="issue details">
9
9
  <%%= render :partial => 'show' , :locals => {:<%= singular_name %> => @<%= singular_name %>} %>
@@ -20,5 +20,5 @@
20
20
 
21
21
  <%% content_for :sidebar do %>
22
22
  <h3>Actions</h3>
23
- <%%= link_to "#{t('Back_to')} #{<%= class_name %>.human_name}", admin_<%= plural_name %>_path %>
23
+ <%%= link_to "#{t('Back_to')} #{<%= class_name %>.model_name.human}", admin_<%= plural_name %>_path %>
24
24
  <%% end %>
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{somatics3-generators}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benjamin Wong"]
12
- s.date = %q{2010-09-15}
12
+ s.date = %q{2010-09-16}
13
13
  s.description = %q{Somatics 3 generators is used to generate a resource with skeleton admin panel}
14
14
  s.email = %q{tkwong@inspiresynergy.com}
15
15
  s.executables = ["somatics", "somatify"]
@@ -96,7 +96,8 @@ environment 'config.autoload_paths += %W(#{config.root}/lib)'
96
96
  generate "somatics:authenticated user"
97
97
  generate "somatics:authenticated_controller admin/user --model=User"
98
98
 
99
- run %(rails runner "User.create(:name => 'Admin', :login => 'admin', :password => 'somatics', :password_confirmation => 'somatics', :email => 'admin@admin.com')")
99
+ run %(rails runner "User.create(:name => 'Admin', :login => 'admin', :password => 'somatics', :password_confirmation => 'somatics', :email => 'admin@admin.com')") if yes?(%(Create Default Admin User (username:admin, password:somatics)?))
100
+
100
101
 
101
102
  #
102
103
  # generate "tinymce_installation"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: somatics3-generators
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benjamin Wong
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-15 00:00:00 +08:00
18
+ date: 2010-09-16 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21