refinerycms-core 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/app/assets/javascripts/refinery/admin.js.erb +28 -33
  2. data/app/assets/javascripts/refinery/modal_dialogs.js.erb +29 -31
  3. data/app/assets/javascripts/refinery/submit_continue.js.coffee.erb +1 -0
  4. data/app/assets/javascripts/wymeditor/lang/nl.js +55 -45
  5. data/app/assets/javascripts/wymeditor/lang/pl.js +12 -1
  6. data/app/controllers/refinery/admin/core_controller.rb +1 -1
  7. data/app/controllers/refinery/admin/dialogs_controller.rb +25 -9
  8. data/app/views/refinery/admin/_form_actions.html.erb +3 -1
  9. data/lib/generators/refinery/dummy/dummy_generator.rb +2 -2
  10. data/lib/generators/refinery/dummy/templates/rails/{application.rb → application.rb.erb} +0 -0
  11. data/lib/generators/refinery/dummy/templates/rails/{boot.rb → boot.rb.erb} +0 -0
  12. data/lib/generators/refinery/engine/engine_generator.rb +5 -1
  13. data/lib/generators/refinery/engine/templates/app/controllers/refinery/namespace/admin/{plural_name_controller.rb → plural_name_controller.rb.erb} +0 -0
  14. data/lib/generators/refinery/engine/templates/app/controllers/refinery/namespace/{plural_name_controller.rb → plural_name_controller.rb.erb} +0 -0
  15. data/lib/generators/refinery/engine/templates/app/models/refinery/namespace/{singular_name.rb → singular_name.rb.erb} +7 -9
  16. data/lib/generators/refinery/engine/templates/app/views/refinery/namespace/admin/plural_name/_singular_name.html.erb +1 -1
  17. data/lib/generators/refinery/engine/templates/config/locales/sk.yml +28 -0
  18. data/lib/generators/refinery/engine/templates/config/{routes.rb → routes.rb.erb} +0 -0
  19. data/lib/generators/refinery/engine/templates/db/migrate/{1_create_namespace_plural_name.rb → 1_create_namespace_plural_name.rb.erb} +0 -0
  20. data/lib/generators/refinery/engine/templates/db/{seeds.rb → seeds.rb.erb} +1 -1
  21. data/lib/generators/refinery/engine/templates/lib/generators/refinery/{extension_plural_name_generator.rb → extension_plural_name_generator.rb.erb} +0 -0
  22. data/lib/generators/refinery/engine/templates/lib/refinery/{plural_name.rb → plural_name.rb.erb} +0 -0
  23. data/lib/generators/refinery/engine/templates/lib/refinery/plural_name/{engine.rb → engine.rb.erb} +0 -0
  24. data/lib/generators/refinery/engine/templates/lib/{refinerycms-extension_plural_name.rb → refinerycms-extension_plural_name.rb.erb} +0 -0
  25. data/lib/generators/refinery/engine/templates/spec/models/refinery/namespace/{singular_name_spec.rb → singular_name_spec.rb.erb} +0 -0
  26. data/lib/generators/refinery/engine/templates/spec/requests/refinery/namespace/admin/{plural_name_spec.rb → plural_name_spec.rb.erb} +6 -6
  27. data/lib/generators/refinery/engine/templates/spec/spec_helper.rb +0 -2
  28. data/lib/generators/refinery/engine/templates/spec/support/factories/refinery/{plural_name.rb → plural_name.rb.erb} +0 -0
  29. data/lib/generators/refinery/form/templates/app/controllers/refinery/namespace/admin/{plural_name_controller.rb → plural_name_controller.rb.erb} +4 -4
  30. data/lib/generators/refinery/form/templates/app/controllers/refinery/namespace/admin/settings_controller.rb.erb +45 -0
  31. data/lib/generators/refinery/form/templates/app/controllers/refinery/namespace/{plural_name_controller.rb → plural_name_controller.rb.erb} +0 -0
  32. data/lib/generators/refinery/form/templates/app/mailers/refinery/namespace/{mailer.rb → mailer.rb.erb} +0 -0
  33. data/lib/generators/refinery/form/templates/app/models/refinery/namespace/{setting.rb → setting.rb.erb} +0 -0
  34. data/lib/generators/refinery/form/templates/app/models/refinery/namespace/{singular_name.rb → singular_name.rb.erb} +19 -14
  35. data/lib/generators/refinery/form/templates/app/views/refinery/namespace/admin/plural_name/_singular_name.html.erb +1 -1
  36. data/lib/generators/refinery/form/templates/app/views/refinery/namespace/admin/settings/_confirmation_email_form.html.erb +1 -1
  37. data/lib/generators/refinery/form/templates/app/views/refinery/namespace/admin/settings/_notification_recipients_form.html.erb +1 -1
  38. data/lib/generators/refinery/form/templates/app/views/refinery/namespace/plural_name/new.html.erb +4 -4
  39. data/lib/generators/refinery/form/templates/config/locales/cs.yml +78 -0
  40. data/lib/generators/refinery/form/templates/config/locales/sk.yml +78 -0
  41. data/lib/generators/refinery/form/templates/config/{routes.rb → routes.rb.erb} +2 -2
  42. data/lib/generators/refinery/form/templates/db/migrate/{1_create_plural_name.rb → 1_create_plural_name.rb.erb} +5 -4
  43. data/lib/generators/refinery/form/templates/db/{seeds.rb → seeds.rb.erb} +1 -1
  44. data/lib/generators/refinery/form/templates/lib/generators/refinery/{plural_name_generator.rb → plural_name_generator.rb.erb} +0 -0
  45. data/lib/generators/refinery/form/templates/lib/refinery/{plural_name.rb → plural_name.rb.erb} +0 -0
  46. data/lib/generators/refinery/form/templates/lib/refinery/plural_name/{engine.rb → engine.rb.erb} +0 -0
  47. data/lib/generators/refinery/form/templates/lib/{refinerycms-plural_name.rb → refinerycms-plural_name.rb.erb} +0 -0
  48. data/lib/refinery/cli.rb +73 -111
  49. data/lib/refinery/core.rb +8 -4
  50. data/lib/refinery/crud.rb +9 -5
  51. data/lib/refinery/extension_generation.rb +250 -214
  52. data/lib/refinery/version.rb +2 -2
  53. data/spec/helpers/refinery/custom_assets_helper_spec.rb +2 -2
  54. data/spec/helpers/refinery/pagination_helper_spec.rb +1 -1
  55. data/spec/lib/generators/refinery/cms/cms_generator_spec.rb +1 -1
  56. data/spec/lib/generators/refinery/core/core_generator_spec.rb +1 -1
  57. data/spec/lib/generators/refinery/engine/engine_generator_spec.rb +9 -4
  58. data/spec/lib/generators/refinery/engine/engine_generator_with_i18n_spec.rb +1 -1
  59. data/spec/lib/generators/refinery/engine/engine_generator_without_frontend_spec.rb +1 -1
  60. data/spec/lib/generators/refinery/form/form_generator_spec.rb +1 -1
  61. data/spec/lib/refinery/application_controller_spec.rb +8 -0
  62. data/spec/lib/refinery/core_spec.rb +14 -5
  63. data/spec/lib/refinery/crud_spec.rb +106 -0
  64. data/spec/lib/refinery/plugin_spec.rb +1 -1
  65. data/spec/lib/refinery/plugins_spec.rb +1 -1
  66. data/spec/requests/refinery/admin/custom_assets_spec.rb +2 -2
  67. data/spec/requests/refinery/admin/dialogs_spec.rb +3 -2
  68. data/spec/requests/refinery/search_spec.rb +2 -2
  69. data/spec/support/database_cleaner.rb +8 -2
  70. data/spec/support/refinery.rb +1 -2
  71. metadata +37 -32
  72. data/lib/generators/refinery/form/templates/app/controllers/refinery/namespace/admin/settings_controller.rb +0 -51
@@ -1,4 +1,4 @@
1
- Refinery::Core::Engine.routes.append do
1
+ Refinery::Core::Engine.routes.draw do
2
2
  # Frontend routes
3
3
  namespace :<%= namespacing.underscore %> do
4
4
  resources :<%= plural_name %><%= ", :path => ''" if namespacing.underscore == plural_name %>, :only => [:new, :create] do
@@ -19,7 +19,7 @@ Refinery::Core::Engine.routes.append do
19
19
  get :toggle_spam
20
20
  end
21
21
  end<% end %>
22
- resources :settings
22
+ resources :settings, :only => [:edit, :update]
23
23
  end
24
24
  end
25
25
  end
@@ -5,12 +5,13 @@ class Create<%= class_name.pluralize %> < ActiveRecord::Migration
5
5
  <%
6
6
  attributes.each do |attribute|
7
7
  # turn image or resource into what it was supposed to be which is an integer reference to an image or resource.
8
- if attribute.type.to_s =~ /^(image|resource)$/
8
+ case attribute.type
9
+ when :image, :resource
9
10
  attribute.type = 'integer'
10
- attribute.name = "#{attribute.name}_id".gsub("_id_id", "_id")
11
- elsif attribute.type.to_s =~ /^(radio|select)$/
11
+ attribute.name = "#{attribute.name}_id"
12
+ when :radio, :select
12
13
  attribute.type = 'string'
13
- elsif attribute.type.to_s =~ /^(checkbox)$/
14
+ when :checkbox
14
15
  attribute.type = 'boolean'
15
16
  end
16
17
  -%>
@@ -1,6 +1,6 @@
1
1
  (Refinery.i18n_enabled? ? Refinery::I18n.frontend_locales : [:en]).each do |lang|
2
2
  I18n.locale = lang
3
-
3
+
4
4
  if defined?(Refinery::User)
5
5
  Refinery::User.all.each do |user|
6
6
  if user.plugins.find_by_name('<%= plural_name %>').nil?
data/lib/refinery/cli.rb CHANGED
@@ -4,123 +4,60 @@ module Refinery
4
4
  class CLI < Thor
5
5
  include Thor::Actions
6
6
 
7
+ OVERRIDES = {
8
+ :view => {
9
+ :glob => '*.{erb,builder}',
10
+ :dir => 'views',
11
+ :desc => 'view template',
12
+ },
13
+ :controller => {
14
+ :glob => '*.rb',
15
+ :dir => 'controllers',
16
+ :desc => 'controller',
17
+ },
18
+ :model => {
19
+ :glob => '*.rb',
20
+ :dir => 'models',
21
+ :desc => 'model',
22
+ },
23
+ :presenter => {
24
+ :glob => '*.rb',
25
+ :dir => 'presenters',
26
+ :desc => 'presenter',
27
+ },
28
+ :javascript => {
29
+ :glob => '*.js{,.*}',
30
+ :dir => 'assets/javascripts',
31
+ :desc => 'javascript',
32
+ },
33
+ :stylesheet => {
34
+ :glob => '*.css.scss',
35
+ :dir => 'assets/stylesheets',
36
+ :desc => 'stylesheet',
37
+ },
38
+ }
39
+
7
40
  desc "override", "copies files from any Refinery extension that you are using into your application"
8
41
  def override(env)
9
- if (view = env["view"]).present?
10
- pattern = "{refinery#{File::SEPARATOR},}#{view.split("/").join(File::SEPARATOR)}*.{erb,builder}"
11
- looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "views", pattern).to_s}
12
-
13
- # copy in the matches
14
- if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
15
- matches.each do |match|
16
- dir = match.split("/app/views/").last.split('/')
17
- file = dir.pop # get rid of the file.
18
- dir = dir.join(File::SEPARATOR) # join directory back together
19
-
20
- destination_dir = Rails.root.join("app", "views", dir)
21
- FileUtils.mkdir_p(destination_dir)
22
- FileUtils.cp match, (destination = File.join(destination_dir, file))
23
-
24
- puts "Copied view template file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
25
- end
26
- else
27
- puts "Couldn't match any view template files in any extensions like #{view}"
28
- end
29
- elsif (controller = env["controller"]).present?
30
- pattern = "{refinery#{File::SEPARATOR},}#{controller.split("/").join(File::SEPARATOR)}*.rb"
31
- looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "controllers", pattern).to_s}
32
-
33
- # copy in the matches
34
- if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
35
- matches.each do |match|
36
- dir = match.split("/app/controllers/").last.split('/')
37
- file = dir.pop # get rid of the file.
38
- dir = dir.join(File::SEPARATOR) # join directory back together
39
-
40
- destination_dir = Rails.root.join("app", "controllers", dir)
41
- FileUtils.mkdir_p(destination_dir)
42
- FileUtils.cp match, (destination = File.join(destination_dir, file))
43
-
44
- puts "Copied controller file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
45
- end
46
- else
47
- puts "Couldn't match any controller files in any extensions like #{controller}"
48
- end
49
- elsif (model = env["model"]).present?
50
- pattern = "{refinery#{File::SEPARATOR},}#{model.split("/").join(File::SEPARATOR)}*.rb"
51
- looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "models", pattern).to_s}
52
-
53
- # copy in the matches
54
- if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
55
- matches.each do |match|
56
- dir = match.split("/app/models/").last.split('/')
57
- file = dir.pop # get rid of the file.
58
- dir = dir.join(File::SEPARATOR) # join directory back together
59
-
60
- destination_dir = Rails.root.join("app", "models", dir)
61
- FileUtils.mkdir_p(destination_dir)
62
- FileUtils.cp match, (destination = File.join(destination_dir, file))
63
-
64
- puts "Copied model file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
65
- end
66
- else
67
- puts "Couldn't match any model files in any extensions like #{model}"
68
- end
69
- elsif (javascripts = env["javascript"]).present?
70
- pattern = "#{javascripts.split("/").join(File::SEPARATOR)}*.js{,.*}"
71
- looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "assets", "javascripts", pattern).to_s}
72
-
73
- # copy in the matches
74
- if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
75
- matches.each do |match|
76
- dir = match.split("/app/assets/javascripts/").last.split('/')
77
- file = dir.pop # get rid of the file.
78
- dir = dir.join(File::SEPARATOR) # join directory back together
79
-
80
- destination_dir = Rails.root.join("app", "assets", "javascripts", dir)
81
- FileUtils.mkdir_p(destination_dir)
82
- FileUtils.cp match, (destination = File.join(destination_dir, file))
83
-
84
- puts "Copied javascript file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
85
- end
86
- else
87
- puts "Couldn't match any javascript files in any extensions like #{javascripts}"
42
+ OVERRIDES.keys.each do |kind|
43
+ if (which = env[kind.to_s]).present?
44
+ return _override(kind, which)
88
45
  end
89
- elsif (stylesheets = env["stylesheet"]).present?
90
- pattern = "#{stylesheets.split("/").join(File::SEPARATOR)}*.css.scss"
91
- looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "assets", "stylesheets", pattern).to_s}
92
-
93
- # copy in the matches
94
- if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
95
- matches.each do |match|
96
- dir = match.split("/app/assets/stylesheets/").last.split('/')
97
- file = dir.pop # get rid of the file.
98
- dir = dir.join(File::SEPARATOR) # join directory back together
99
-
100
- destination_dir = Rails.root.join("app", "assets", "stylesheets", dir)
101
- FileUtils.mkdir_p(destination_dir)
102
- FileUtils.cp match, (destination = File.join(destination_dir, file))
46
+ end
103
47
 
104
- puts "Copied stylesheet file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
105
- end
106
- else
107
- puts "Couldn't match any stylesheet files in any extensions like #{stylesheets}"
108
- end
109
- else
110
- puts "You didn't specify anything to override. Here are some examples:"
111
- {
112
- :view => ['pages/home', 'refinery/pages/home', '**/*menu', '_menu_branch'],
113
- :javascript => %w(admin refinery/site_bar),
114
- :stylesheet => %w(home refinery/site_bar),
115
- :controller => %w(pages),
116
- :model => %w(page refinery/page)
117
- }.each do |type, examples|
118
- examples.each do |example|
119
- puts "rake refinery:override #{type}=#{example}"
120
- end
48
+ puts "You didn't specify anything valid to override. Here are some examples:"
49
+ {
50
+ :view => ['pages/home', 'refinery/pages/home', '**/*menu', '_menu_branch'],
51
+ :javascript => %w(admin refinery/site_bar),
52
+ :stylesheet => %w(home refinery/site_bar),
53
+ :controller => %w(pages),
54
+ :model => %w(page refinery/page),
55
+ :presenter => %w(refinery/page_presenter)
56
+ }.each do |type, examples|
57
+ examples.each do |example|
58
+ puts "rake refinery:override #{type}=#{example}"
121
59
  end
122
60
  end
123
-
124
61
  end
125
62
 
126
63
  desc "uncrudify", "shows you the code that your controller using crudify is running for a given action"
@@ -158,5 +95,30 @@ module Refinery
158
95
  puts crud_method
159
96
  end
160
97
  end
98
+
99
+ private
100
+
101
+ def _override(kind, which)
102
+ override_kind = OVERRIDES[kind]
103
+ pattern = "{refinery#{File::SEPARATOR},}#{which.split("/").join(File::SEPARATOR)}#{override_kind[:glob]}"
104
+ looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", override_kind[:dir], pattern).to_s}
105
+
106
+ # copy in the matches
107
+ if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
108
+ matches.each do |match|
109
+ dir = match.split("/app/#{override_kind[:dir]}/").last.split('/')
110
+ file = dir.pop # get rid of the file.
111
+ dir = dir.join(File::SEPARATOR) # join directory back together
112
+
113
+ destination_dir = Rails.root.join('app', override_kind[:dir], dir)
114
+ FileUtils.mkdir_p(destination_dir)
115
+ FileUtils.cp match, (destination = File.join(destination_dir, file))
116
+
117
+ puts "Copied #{override_kind[:desc]} file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
118
+ end
119
+ else
120
+ puts "Couldn't match any #{override_kind[:desc]} files in any extensions like #{which}"
121
+ end
122
+ end
161
123
  end
162
124
  end
data/lib/refinery/core.rb CHANGED
@@ -128,7 +128,7 @@ module Refinery
128
128
  # Refinery.roots(:'refinery/core') => #<Pathname:/Users/Reset/Code/refinerycms/core>
129
129
  # Refinery.roots("refinery/core") => #<Pathname:/Users/Reset/Code/refinerycms/core>
130
130
  def roots(extension_name = nil)
131
- return @roots ||= self.extensions.map { |extension| extension.root } if extension_name.nil?
131
+ return @roots ||= self.extensions.map(&:root) if extension_name.nil?
132
132
 
133
133
  extension_name.to_s.camelize.constantize.root
134
134
  end
@@ -157,13 +157,17 @@ module Refinery
157
157
  options = {:plural => false, :admin => true}.merge options
158
158
 
159
159
  klass = klass.constantize if klass.respond_to?(:constantize)
160
- active_name = ActiveModel::Name.new(klass, (Refinery if klass.parents.include?(Refinery)))
160
+ active_name = ActiveModel::Name.new klass, (Refinery if klass.parents.include?(Refinery))
161
161
 
162
162
  if options[:admin]
163
163
  # Most of the time this gets rid of 'refinery'
164
- parts = active_name.underscore.split('/').reject{|name| active_name.singular_route_key.exclude?(name)}
164
+ parts = active_name.underscore.split('/').reject{|name|
165
+ active_name.singular_route_key.exclude?(name)
166
+ }
167
+
168
+ # Get the singular resource_name from the url parts
165
169
  resource_name = parts.pop
166
- resource_name = options[:plural] ? resource_name.pluralize : resource_name.singularize
170
+ resource_name = resource_name.pluralize if options[:plural]
167
171
 
168
172
  [parts.join("_"), "admin", resource_name, "path"].reject(&:blank?).join "_"
169
173
  else
data/lib/refinery/crud.rb CHANGED
@@ -266,8 +266,11 @@ module Refinery
266
266
  def update_positions
267
267
  previous = nil
268
268
  params[:ul].each do |_, list|
269
- list.each do |index, hash|
270
- moved_item_id = hash['id'].split(/#{singular_name}\\_?/)
269
+ # After we drop Ruby 1.8.x support the following line can be changed back to
270
+ # list.each do |index, hash|
271
+ # because there won't be an ordering issue (see https://github.com/resolve/refinerycms/issues/1585)
272
+ list.sort.map { |item| item[1] }.each_with_index do |hash, index|
273
+ moved_item_id = hash['id'].split(/#{singular_name}\_?/).reject(&:empty?).first
271
274
  @current_#{singular_name} = #{class_name}.find_by_id(moved_item_id)
272
275
 
273
276
  if @current_#{singular_name}.respond_to?(:move_to_root)
@@ -277,7 +280,7 @@ module Refinery
277
280
  @current_#{singular_name}.move_to_root
278
281
  end
279
282
  else
280
- @current_#{singular_name}.update_attribute(:position, index)
283
+ @current_#{singular_name}.update_attributes :position => index
281
284
  end
282
285
 
283
286
  if hash['children'].present?
@@ -293,8 +296,9 @@ module Refinery
293
296
  end
294
297
 
295
298
  def update_child_positions(_node, #{singular_name})
296
- _node['children']['0'].each do |_, child|
297
- child_id = child['id'].split(/#{singular_name}\_?/)
299
+ list = _node['children']['0']
300
+ list.sort.map { |item| item[1] }.each_with_index do |child, index|
301
+ child_id = child['id'].split(/#{singular_name}\_?/).reject(&:empty?).first
298
302
  child_#{singular_name} = #{class_name}.where(:id => child_id).first
299
303
  child_#{singular_name}.move_to_child_of(#{singular_name})
300
304
 
@@ -10,271 +10,307 @@ module Refinery
10
10
  class_option :i18n, :type => :array, :default => [], :required => false, :banner => "field field", :desc => 'Indicates generated fields'
11
11
 
12
12
  remove_class_option :skip_namespace
13
+ end
14
+ end
13
15
 
14
- def namespacing
15
- @namespacing ||= if options[:namespace].present?
16
- # Use exactly what the user requested, not a pluralised version.
17
- options[:namespace].to_s.camelize
18
- else
19
- class_name.pluralize
20
- end
21
- end
16
+ def namespacing
17
+ @namespacing ||= if options[:namespace].present?
18
+ # Use exactly what the user requested, not a pluralised version.
19
+ options[:namespace].to_s.camelize
20
+ else
21
+ class_name.pluralize
22
+ end
23
+ end
22
24
 
23
- def extension_name
24
- @extension_name ||= if options[:extension].present?
25
- # Use exactly what the user requested, not a made up version.
26
- options[:extension].to_s
27
- else
28
- singular_name
29
- end
30
- end
25
+ def extension_name
26
+ @extension_name ||= options[:extension].presence || singular_name
27
+ end
31
28
 
32
- def extension_class_name
33
- @extension_class_name ||= extension_name.camelize
34
- end
29
+ def extension_class_name
30
+ @extension_class_name ||= extension_name.camelize
31
+ end
35
32
 
36
- def extension_plural_class_name
37
- @extension_plural_class_name ||= if options[:extension].present?
38
- # Use exactly what the user requested, not a plural version.
39
- extension_class_name
40
- else
41
- extension_class_name.pluralize
42
- end
43
- end
33
+ def extension_plural_class_name
34
+ @extension_plural_class_name ||= if options[:extension].present?
35
+ # Use exactly what the user requested, not a plural version.
36
+ extension_class_name
37
+ else
38
+ extension_class_name.pluralize
39
+ end
40
+ end
44
41
 
45
- def extension_plural_name
46
- @extension_plural_name ||= if options[:extension].present?
47
- # Use exactly what the user requested, not a plural version.
48
- extension_name
49
- else
50
- extension_name.pluralize
51
- end
52
- end
42
+ def extension_plural_name
43
+ @extension_plural_name ||= if options[:extension].present?
44
+ # Use exactly what the user requested, not a plural version.
45
+ extension_name
46
+ else
47
+ extension_name.pluralize
48
+ end
49
+ end
53
50
 
54
- def localized?
55
- localized_attributes.any?
56
- end
51
+ def localized?
52
+ localized_attributes.any?
53
+ end
57
54
 
58
- def localized_attributes
59
- @localized_attributes ||= attributes.select{|a| options[:i18n].include?(a.name)}
60
- end
55
+ def localized_attributes
56
+ @localized_attributes ||= attributes.select{|a| options[:i18n].include?(a.name)}
57
+ end
61
58
 
62
- def attributes_for_translation_table
63
- localized_attributes.inject([]) {|memo, attr| memo << ":#{attr.name} => :#{attr.type}"}.join(', ')
64
- end
59
+ def attributes_for_translation_table
60
+ localized_attributes.inject([]) {|memo, attr| memo << ":#{attr.name} => :#{attr.type}"}.join(', ')
61
+ end
65
62
 
66
- protected
63
+ def string_attributes
64
+ @string_attributes ||= attributes.select {|a| /string|text/ === a.type.to_s}.uniq
65
+ end
67
66
 
68
- def append_extension_to_gemfile!
69
- unless Rails.env.test? || (self.behavior != :revoke && extension_in_gemfile?)
70
- path = extension_pathname.parent.relative_path_from(gemfile.parent)
71
- append_file gemfile, "\ngem '#{gem_name}', :path => '#{path}'"
72
- end
73
- end
67
+ def image_attributes
68
+ @image_attributes ||= attributes.select { |a| a.type == :image }.uniq
69
+ end
74
70
 
75
- def clash_keywords
76
- @clash_keywords ||= begin
77
- clash_keywords = []
78
- unless (clash_file = source_pathname.parent.join('clash_keywords.yml')).file?
79
- clash_file = source_pathname.parent.parent.join('clash_keywords.yml')
80
- end
81
- clash_keywords = YAML.load_file(clash_file) if clash_file.file?
82
- clash_keywords
83
- end
84
- end
71
+ def resource_attributes
72
+ @resource_attributes ||= attributes.select { |a| a.type == :resource }.uniq
73
+ end
85
74
 
86
- def destination_pathname
87
- @destination_pathname ||= Pathname.new(self.destination_root.to_s)
75
+ def names_for_attr_accessible
76
+ @attributes_for_attr_accessible ||= attributes.map do |a|
77
+ case a.type
78
+ when :image, :resource
79
+ "#{a.name}_id" unless a.name[-3..-1] == "_id"
80
+ else
81
+ a.name
88
82
  end
83
+ end
84
+ end
85
+
86
+ protected
89
87
 
90
- def extension_pathname
91
- destination_pathname.join('vendor', 'extensions', extension_plural_name)
88
+ def append_extension_to_gemfile!
89
+ unless Rails.env.test? || (self.behavior != :revoke && extension_in_gemfile?)
90
+ path = extension_pathname.parent.relative_path_from(gemfile.parent)
91
+ append_file gemfile, "\ngem '#{gem_name}', :path => '#{path}'"
92
+ end
93
+ end
94
+
95
+ def clash_keywords
96
+ @clash_keywords ||= begin
97
+ clash_keywords = []
98
+ unless (clash_file = source_pathname.parent.join('clash_keywords.yml')).file?
99
+ clash_file = source_pathname.parent.parent.join('clash_keywords.yml')
92
100
  end
101
+ clash_keywords = YAML.load_file(clash_file) if clash_file.file?
102
+ clash_keywords
103
+ end
104
+ end
93
105
 
94
- def extension_path_for(path, extension)
95
- path = extension_pathname.join(path.to_s.gsub(%r{#{source_pathname}/?}, '')).to_s
96
-
97
- path.gsub!('extension_plural_name', extension_plural_name)
98
- path.gsub!('plural_name', plural_name)
99
- path.gsub!('singular_name', singular_name)
100
- path.gsub!('namespace', namespacing.underscore)
101
-
102
- if options[:namespace].present? || options[:extension].present?
103
- # Increment the migration file leading number
104
- # Only relevant for nested or namespaced extensions, where a previous migration exists
105
- if path =~ %r{/migrate/\d+\w*.rb\z}
106
- if last_migration = Dir["#{destination_pathname.join(path.split(File::SEPARATOR)[0..-2].join(File::SEPARATOR), '*.rb')}"].sort.last
107
- path.gsub!(%r{\d+_}) { |m| "#{last_migration.match(%r{migrate/(\d+)_})[1].to_i + 1}_" }
108
- end
109
- end
106
+ def destination_pathname
107
+ @destination_pathname ||= Pathname.new(self.destination_root.to_s)
108
+ end
110
109
 
111
- # Detect whether this is a special file that needs to get merged not overwritten.
112
- # This is important only when nesting extensions.
113
- if extension.present? && File.exist?(path)
114
- path = if path =~ %r{/locales/.*\.yml$} or path =~ %r{/routes.rb$} or path =~ %r{/#{gem_name}.rb$}
115
- # put new translations into a tmp directory
116
- path.split(File::SEPARATOR).insert(-2, "tmp").join(File::SEPARATOR)
117
- elsif path =~ %r{/readme.md$} or path =~ %r{/#{plural_name}.rb$}
118
- nil
119
- else
120
- path
121
- end
122
- elsif extension.present? and path =~ /lib\/#{plural_name}.rb$/
123
- path = nil
124
- end
125
- end
110
+ def extension_pathname
111
+ destination_pathname.join('vendor', 'extensions', extension_plural_name)
112
+ end
126
113
 
127
- path
128
- end
114
+ def extension_path_for(path, extension, apply_tmp = true)
115
+ path = extension_pathname.join(path.to_s.gsub(%r{#{source_pathname}/?}, '')).to_s
129
116
 
130
- def erase_destination!
131
- if Pathname.glob(extension_pathname.join('**', '*')).all?(&:directory?)
132
- say_status :remove, relative_to_original_destination_root(extension_pathname.to_s), true
133
- FileUtils.rm_rf extension_pathname unless options[:pretend]
117
+ path.gsub!('extension_plural_name', extension_plural_name)
118
+ path.gsub!('plural_name', plural_name)
119
+ path.gsub!('singular_name', singular_name)
120
+ path.gsub!('namespace', namespacing.underscore)
121
+
122
+ if options[:namespace].present? || options[:extension].present?
123
+ # Increment the migration file leading number
124
+ # Only relevant for nested or namespaced extensions, where a previous migration exists
125
+ if %r{/migrate/\d+\w*.rb\z} === path
126
+ if last_migration = Dir["#{destination_pathname.join(path.split(File::SEPARATOR)[0..-2].join(File::SEPARATOR), '*.rb')}"].sort.last
127
+ path.gsub!(%r{\d+_}) { |m| "#{last_migration.match(%r{migrate/(\d+)_})[1].to_i + 1}_" }
134
128
  end
135
129
  end
136
130
 
137
- def evaluate_templates!
138
- Pathname.glob(source_pathname.join('**', '**')).reject{|f|
139
- reject_template?(f)
140
- }.sort.each do |path|
141
- if (template_path = extension_path_for(path, extension_name)).present?
142
- next if path.to_s =~ /seeds.rb/
143
- template path, template_path
131
+ # Detect whether this is a special file that needs to get merged not overwritten.
132
+ # This is important only when nesting extensions.
133
+ if extension.present? && File.exist?(path)
134
+ if %r{/locales/.*\.yml$} === path ||
135
+ %r{/routes.rb$} === path ||
136
+ %r{/#{gem_name}.rb$} === path
137
+ # put new translations into a tmp directory
138
+ if apply_tmp
139
+ path = path.split(File::SEPARATOR).insert(-2, "tmp").
140
+ join(File::SEPARATOR)
144
141
  end
142
+ elsif %r{/readme.md$} === path || %r{/#{plural_name}.rb$} === path
143
+ path = nil
145
144
  end
145
+ elsif extension.present? and path =~ /lib\/#{plural_name}.rb$/
146
+ path = nil
146
147
  end
148
+ end
147
149
 
148
- def existing_extension?
149
- options[:extension].present? && extension_pathname.directory? && extension_in_gemfile?
150
- end
150
+ path.present? ? Pathname.new(path) : path
151
+ end
151
152
 
152
- def exit_with_message!(message)
153
- puts ""
154
- puts message
155
- puts ""
156
- exit(1)
157
- end
153
+ def erase_destination!
154
+ if Pathname.glob(extension_pathname.join('**', '*')).all?(&:directory?)
155
+ say_status :remove, relative_to_original_destination_root(extension_pathname.to_s), true
156
+ FileUtils.rm_rf extension_pathname unless options[:pretend]
157
+ end
158
+ end
158
159
 
159
- def extension_in_gemfile?
160
- gemfile.read.scan(%r{#{gem_name}}).any?
161
- end
160
+ def evaluate_templates!
161
+ Pathname.glob(source_pathname.join('**', '**')).reject{|f|
162
+ reject_template?(f)
163
+ }.sort.each do |path|
164
+ if (template_path = extension_path_for(path, extension_name)).present?
165
+ next if path.to_s =~ /seeds.rb.erb/
162
166
 
163
- def finalize_extension!
164
- if self.behavior != :revoke and !self.options['pretend']
165
- puts_instructions!
166
- else
167
- erase_destination!
167
+ unless path.to_s =~ /views/
168
+ template_path = template_path.to_s.sub(".erb", "")
168
169
  end
169
- end
170
170
 
171
- def gem_name
172
- "refinerycms-#{extension_plural_name}"
173
- end
174
-
175
- def gemfile
176
- @gemfile ||= begin
177
- Bundler.default_gemfile || destination_pathname.join('Gemfile')
178
- end
171
+ template path, template_path
179
172
  end
173
+ end
174
+ end
180
175
 
181
- def merge_locales!
182
- if existing_extension?
183
- # go through all of the temporary files and merge what we need into the current files.
184
- tmp_directories = []
185
- Dir.glob(source_pathname.join("{config/locales/*.yml,config/routes.rb,lib/refinerycms-extension_plural_name.rb}"), File::FNM_DOTMATCH).sort.each do |path|
186
- # get the path to the current tmp file.
187
- new_file_path = destination_pathname.join(extension_path_for(path, extension_name))
188
- tmp_directories << Pathname.new(new_file_path.to_s.split(File::SEPARATOR)[0..-2].join(File::SEPARATOR)) # save for later
189
- # get the path to the existing file and perform a deep hash merge.
190
- current_path = Pathname.new(new_file_path.to_s.split(File::SEPARATOR).reject{|f| f == 'tmp'}.join(File::SEPARATOR))
191
- new_contents = nil
192
- if new_file_path.to_s =~ %r{.yml$}
193
- # merge translation files together.
194
- new_contents = YAML::load(new_file_path.read).deep_merge(YAML::load(current_path.read)).to_yaml.gsub(%r{^---\n}, '')
195
- elsif new_file_path.to_s =~ %r{/routes.rb$}
196
- # append any routes from the new file to the current one.
197
- routes_file = [(file_parts = current_path.read.to_s.split("\n")).first]
198
- routes_file += file_parts[1..-2]
199
- routes_file += new_file_path.read.to_s.split("\n")[1..-2]
200
- routes_file << file_parts.last
201
- new_contents = routes_file.join("\n")
202
- elsif new_file_path.to_s =~ %r{/#{gem_name}.rb$}
203
- new_contents = current_path.read + new_file_path.read
204
- end
205
- # write to current file the merged results.
206
- current_path.open('w+') { |file| file.puts new_contents } if new_contents
207
- end
176
+ def existing_extension?
177
+ options[:extension].present? && extension_pathname.directory?
178
+ end
208
179
 
209
- tmp_directories.uniq.each{|dir| remove_dir(dir) if dir && dir.exist?}
210
- end
211
- end
180
+ def exit_with_message!(message)
181
+ puts ""
182
+ puts message
183
+ puts ""
184
+ exit 1
185
+ end
212
186
 
213
- def copy_or_merge_seeds!
214
- source_seed_file = source_pathname.join("db/seeds.rb")
215
- destination_seed_file = destination_pathname.join(extension_path_for(source_seed_file, extension_name))
187
+ def finalize_extension!
188
+ if self.behavior != :revoke and !self.options['pretend']
189
+ puts_instructions!
190
+ else
191
+ erase_destination!
192
+ end
193
+ end
216
194
 
217
- if existing_extension?
218
- # create temp seeds file
219
- temp_seed_file = destination_pathname.join(extension_path_for("tmp/seeds.rb", extension_name))
195
+ def gem_name
196
+ "refinerycms-#{extension_plural_name}"
197
+ end
220
198
 
221
- # copy/evaluate seeds template to temp file
222
- template source_seed_file, temp_seed_file, :verbose => false
199
+ def gemfile
200
+ @gemfile ||= begin
201
+ Bundler.default_gemfile || destination_pathname.join('Gemfile')
202
+ end
203
+ end
223
204
 
224
- # append temp seeds file content to extension seeds file
225
- destination_seed_file.open('a+') { |file| file.puts temp_seed_file.read.to_s }
205
+ def extension_in_gemfile?
206
+ gemfile.read.scan(%r{#{gem_name}}).any?
207
+ end
226
208
 
227
- # remove temp file
228
- FileUtils.rm_rf temp_seed_file
229
- else
230
- template source_seed_file, destination_seed_file
209
+ def merge_locales!
210
+ if existing_extension?
211
+ # go through all of the temporary files and merge what we need into the current files.
212
+ tmp_directories = []
213
+ Dir.glob(source_pathname.join("{config/locales/*.yml,config/routes.rb,lib/refinerycms-extension_plural_name.rb}"), File::FNM_DOTMATCH).sort.each do |path|
214
+ # get the path to the current tmp file.
215
+ new_file_path = extension_path_for(path, extension_name)
216
+ tmp_directories << Pathname.new(new_file_path.to_s.split(File::SEPARATOR)[0..-2].join(File::SEPARATOR)) # save for later
217
+ # get the path to the existing file and perform a deep hash merge.
218
+ current_path = extension_path_for(path, extension_name, false)
219
+ new_contents = nil
220
+ if File.exist?(new_file_path) && %r{.yml$} === new_file_path.to_s
221
+ # merge translation files together.
222
+ new_contents = YAML::load(new_file_path.read).deep_merge(
223
+ YAML::load(current_path.read)
224
+ ).to_yaml.gsub(%r{^---\n}, '')
225
+ elsif %r{/routes.rb$} === new_file_path.to_s
226
+ # append any routes from the new file to the current one.
227
+ routes_file = [(file_parts = current_path.read.to_s.split("\n")).first]
228
+ routes_file += file_parts[1..-2]
229
+ routes_file += new_file_path.read.to_s.split("\n")[1..-2]
230
+ routes_file << file_parts.last
231
+ new_contents = routes_file.join("\n")
232
+ elsif %r{/#{gem_name}.rb$} === new_file_path.to_s
233
+ new_contents = current_path.read + new_file_path.read
231
234
  end
235
+ # write to current file the merged results.
236
+ current_path.open('w+') { |file| file.puts new_contents } if new_contents
232
237
  end
233
238
 
234
- def puts_instructions!
235
- unless Rails.env.test?
236
- puts "------------------------"
237
- puts "Now run:"
238
- puts "bundle install"
239
- puts "rails generate refinery:#{extension_plural_name}"
240
- puts "rake db:migrate"
241
- puts "rake db:seed"
242
- puts "------------------------"
243
- end
244
- end
239
+ tmp_directories.uniq.each{|dir| remove_dir(dir) if dir && dir.exist?}
240
+ end
241
+ end
245
242
 
246
- def reject_file?(file)
247
- !localized? && file.to_s.include?('locale_picker')
248
- end
243
+ def copy_or_merge_seeds!
244
+ source_seed_file = source_pathname.join("db/seeds.rb.erb")
245
+ destination_seed_file = destination_pathname.join(
246
+ extension_path_for(
247
+ source_seed_file.to_s.sub(".erb", ""), extension_name
248
+ )
249
+ )
250
+
251
+ if existing_extension?
252
+ # create temp seeds file
253
+ temp_seed_file = destination_pathname.join(
254
+ extension_path_for("tmp/seeds.rb", extension_name)
255
+ )
256
+
257
+ # copy/evaluate seeds template to temp file
258
+ template source_seed_file, temp_seed_file, :verbose => false
259
+
260
+ # append temp seeds file content to extension seeds file
261
+ destination_seed_file.open('a+') { |file|
262
+ file.puts temp_seed_file.read.to_s
263
+ }
264
+
265
+ # remove temp file
266
+ FileUtils.rm_rf temp_seed_file
267
+ else
268
+ template source_seed_file, destination_seed_file
269
+ end
270
+ end
249
271
 
250
- def reject_template?(file)
251
- file.directory? || reject_file?(file)
252
- end
272
+ def puts_instructions!
273
+ unless Rails.env.test?
274
+ puts "------------------------"
275
+ puts "Now run:"
276
+ puts "bundle install"
277
+ puts "rails generate refinery:#{extension_plural_name}"
278
+ puts "rake db:migrate"
279
+ puts "rake db:seed"
280
+ puts "------------------------"
281
+ end
282
+ end
253
283
 
254
- def sanity_check!
255
- if clash_keywords.member?(singular_name.downcase)
256
- exit_with_message!("Please choose a different name. The generated code would fail for class '#{singular_name}' as it conflicts with a reserved keyword.")
257
- end
284
+ def reject_file?(file)
285
+ !localized? && file.to_s.include?('locale_picker')
286
+ end
258
287
 
259
- if singular_name == plural_name
260
- message = if singular_name.singularize == singular_name
261
- "The extension name you specified will not work as the singular name is equal to the plural name."
262
- else
263
- "Please specify the singular name '#{singular_name.singularize}' instead of '#{plural_name}'."
264
- end
265
- exit_with_message!(message)
266
- end
288
+ def reject_template?(file)
289
+ file.directory? || reject_file?(file)
290
+ end
267
291
 
268
- if attributes.empty? && self.behavior != :revoke
269
- exit_with_message!("You must specify a name and at least one field. For help: #{generator_command}")
270
- end
271
- end
292
+ def sanity_check!
293
+ if clash_keywords.member?(singular_name.downcase)
294
+ exit_with_message!("Please choose a different name. The generated code would fail for class '#{singular_name}' as it conflicts with a reserved keyword.")
295
+ end
272
296
 
273
- def source_pathname
274
- @source_pathname ||= Pathname.new(self.class.source_root.to_s)
297
+ if singular_name == plural_name
298
+ message = if singular_name.singularize == singular_name
299
+ "The extension name you specified will not work as the singular name is equal to the plural name."
300
+ else
301
+ "Please specify the singular name '#{singular_name.singularize}' instead of '#{plural_name}'."
275
302
  end
303
+ exit_with_message!(message)
304
+ end
305
+
306
+ if attributes.empty? && self.behavior != :revoke
307
+ exit_with_message!("You must specify a name and at least one field. For help: #{generator_command}")
276
308
  end
277
309
  end
278
310
 
311
+ def source_pathname
312
+ @source_pathname ||= Pathname.new(self.class.source_root.to_s)
313
+ end
314
+
279
315
  end
280
316
  end