active_scaffold 3.4.43 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (216) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +39 -0
  3. data/{LICENSE → LICENSE.md} +1 -1
  4. data/README.md +27 -19
  5. data/app/assets/javascripts/active_scaffold.js.erb +1 -1
  6. data/app/assets/javascripts/jquery/active_scaffold.js +95 -43
  7. data/app/assets/javascripts/jquery/tiny_mce_bridge.js +30 -6
  8. data/app/assets/javascripts/prototype/tiny_mce_bridge.js +11 -1
  9. data/app/assets/stylesheets/active_scaffold_colors.scss +2 -2
  10. data/app/assets/stylesheets/active_scaffold_layout.css +36 -28
  11. data/app/views/active_scaffold_overrides/_base_form.html.erb +2 -3
  12. data/app/views/active_scaffold_overrides/_field_search.html.erb +8 -7
  13. data/app/views/active_scaffold_overrides/_form_association.html.erb +9 -9
  14. data/app/views/active_scaffold_overrides/_form_association_footer.html.erb +6 -6
  15. data/app/views/active_scaffold_overrides/_form_association_record.html.erb +52 -50
  16. data/app/views/active_scaffold_overrides/_horizontal_subform.html.erb +1 -1
  17. data/app/views/active_scaffold_overrides/_horizontal_subform_header.html.erb +1 -1
  18. data/app/views/active_scaffold_overrides/_human_conditions.html.erb +3 -1
  19. data/app/views/active_scaffold_overrides/_list_calculations.html.erb +1 -1
  20. data/app/views/active_scaffold_overrides/_list_column_headings.html.erb +2 -0
  21. data/app/views/active_scaffold_overrides/_list_messages.html.erb +5 -3
  22. data/app/views/active_scaffold_overrides/_list_record.html.erb +3 -1
  23. data/app/views/active_scaffold_overrides/_list_with_header.html.erb +9 -9
  24. data/app/views/active_scaffold_overrides/_messages.html.erb +1 -1
  25. data/app/views/active_scaffold_overrides/_refresh_list.js.erb +18 -10
  26. data/app/views/active_scaffold_overrides/_render_field.js.erb +3 -3
  27. data/app/views/active_scaffold_overrides/_search.html.erb +7 -6
  28. data/app/views/active_scaffold_overrides/_show_actions.html.erb +14 -0
  29. data/app/views/active_scaffold_overrides/_show_association.html.erb +1 -1
  30. data/app/views/active_scaffold_overrides/_update_actions.html.erb +6 -2
  31. data/app/views/active_scaffold_overrides/_update_column.js.erb +1 -1
  32. data/app/views/active_scaffold_overrides/_update_form.html.erb +1 -1
  33. data/app/views/active_scaffold_overrides/destroy.js.erb +2 -3
  34. data/app/views/active_scaffold_overrides/edit_associated.js.erb +4 -3
  35. data/app/views/active_scaffold_overrides/on_action_update.js.erb +5 -3
  36. data/app/views/active_scaffold_overrides/on_create.js.erb +4 -4
  37. data/app/views/active_scaffold_overrides/on_update.js.erb +6 -6
  38. data/app/views/active_scaffold_overrides/show.html.erb +6 -0
  39. data/app/views/active_scaffold_overrides/update.html.erb +1 -1
  40. data/app/views/active_scaffold_overrides/update_column.js.erb +1 -1
  41. data/config/brakeman.ignore +26 -0
  42. data/config/brakeman.yml +3 -0
  43. data/config/i18n-tasks.yml +121 -0
  44. data/config/locales/de.yml +81 -70
  45. data/config/locales/en.yml +83 -74
  46. data/config/locales/es.yml +82 -73
  47. data/config/locales/fr.yml +86 -75
  48. data/config/locales/hu.yml +81 -70
  49. data/config/locales/ja.yml +71 -60
  50. data/config/locales/ru.yml +85 -74
  51. data/lib/active_scaffold.rb +3 -0
  52. data/lib/active_scaffold/actions/common_search.rb +11 -7
  53. data/lib/active_scaffold/actions/core.rb +119 -47
  54. data/lib/active_scaffold/actions/create.rb +1 -1
  55. data/lib/active_scaffold/actions/delete.rb +11 -8
  56. data/lib/active_scaffold/actions/field_search.rb +104 -6
  57. data/lib/active_scaffold/actions/list.rb +25 -21
  58. data/lib/active_scaffold/actions/mark.rb +12 -4
  59. data/lib/active_scaffold/actions/nested.rb +26 -26
  60. data/lib/active_scaffold/actions/search.rb +2 -2
  61. data/lib/active_scaffold/actions/show.rb +4 -5
  62. data/lib/active_scaffold/actions/subform.rb +9 -7
  63. data/lib/active_scaffold/actions/update.rb +20 -13
  64. data/lib/active_scaffold/active_record_permissions.rb +24 -5
  65. data/lib/active_scaffold/attribute_params.rb +68 -49
  66. data/lib/active_scaffold/bridges.rb +1 -1
  67. data/lib/active_scaffold/bridges/ancestry/ancestry_bridge.rb +15 -19
  68. data/lib/active_scaffold/bridges/bitfields.rb +1 -1
  69. data/lib/active_scaffold/bridges/bitfields/bitfields_bridge.rb +10 -14
  70. data/lib/active_scaffold/bridges/calendar_date_select.rb +0 -7
  71. data/lib/active_scaffold/bridges/calendar_date_select/as_cds_bridge.rb +19 -22
  72. data/lib/active_scaffold/bridges/cancan.rb +4 -3
  73. data/lib/active_scaffold/bridges/cancan/cancan_bridge.rb +11 -21
  74. data/lib/active_scaffold/bridges/carrierwave.rb +2 -1
  75. data/lib/active_scaffold/bridges/carrierwave/carrierwave_bridge.rb +2 -6
  76. data/lib/active_scaffold/bridges/carrierwave/form_ui.rb +6 -39
  77. data/lib/active_scaffold/bridges/carrierwave/list_ui.rb +1 -1
  78. data/lib/active_scaffold/bridges/chosen.rb +4 -1
  79. data/lib/active_scaffold/bridges/chosen/helpers.rb +3 -2
  80. data/lib/active_scaffold/bridges/country_select/country_select_bridge_helper.rb +2 -2
  81. data/lib/active_scaffold/bridges/date_picker.rb +3 -0
  82. data/lib/active_scaffold/bridges/date_picker/ext.rb +43 -38
  83. data/lib/active_scaffold/bridges/date_picker/helper.rb +24 -23
  84. data/lib/active_scaffold/bridges/dragonfly.rb +1 -1
  85. data/lib/active_scaffold/bridges/dragonfly/dragonfly_bridge.rb +3 -7
  86. data/lib/active_scaffold/bridges/dragonfly/form_ui.rb +3 -25
  87. data/lib/active_scaffold/bridges/dragonfly/list_ui.rb +2 -2
  88. data/lib/active_scaffold/bridges/file_column/as_file_column_bridge.rb +6 -8
  89. data/lib/active_scaffold/bridges/file_column/file_column_helpers.rb +1 -1
  90. data/lib/active_scaffold/bridges/file_column/form_ui.rb +0 -2
  91. data/lib/active_scaffold/bridges/file_column/list_ui.rb +2 -1
  92. data/lib/active_scaffold/bridges/file_column/test/test_helper.rb +1 -1
  93. data/lib/active_scaffold/bridges/paper_trail/actions.rb +1 -1
  94. data/lib/active_scaffold/bridges/paper_trail/helper.rb +1 -2
  95. data/lib/active_scaffold/bridges/paper_trail/paper_trail_bridge.rb +3 -7
  96. data/lib/active_scaffold/bridges/paperclip.rb +1 -1
  97. data/lib/active_scaffold/bridges/paperclip/form_ui.rb +3 -28
  98. data/lib/active_scaffold/bridges/paperclip/list_ui.rb +1 -1
  99. data/lib/active_scaffold/bridges/paperclip/paperclip_bridge.rb +3 -7
  100. data/lib/active_scaffold/bridges/record_select.rb +2 -0
  101. data/lib/active_scaffold/bridges/record_select/helpers.rb +14 -18
  102. data/lib/active_scaffold/bridges/semantic_attributes/column.rb +4 -8
  103. data/lib/active_scaffold/bridges/shared/date_bridge.rb +20 -20
  104. data/lib/active_scaffold/bridges/tiny_mce/helpers.rb +7 -22
  105. data/lib/active_scaffold/bridges/usa_state_select/usa_state_select_helper.rb +14 -14
  106. data/lib/active_scaffold/config/base.rb +9 -6
  107. data/lib/active_scaffold/config/core.rb +30 -21
  108. data/lib/active_scaffold/config/create.rb +2 -1
  109. data/lib/active_scaffold/config/delete.rb +2 -2
  110. data/lib/active_scaffold/config/field_search.rb +9 -3
  111. data/lib/active_scaffold/config/form.rb +4 -4
  112. data/lib/active_scaffold/config/list.rb +27 -23
  113. data/lib/active_scaffold/config/nested.rb +4 -4
  114. data/lib/active_scaffold/config/search.rb +6 -6
  115. data/lib/active_scaffold/config/show.rb +11 -1
  116. data/lib/active_scaffold/config/subform.rb +1 -1
  117. data/lib/active_scaffold/config/update.rb +4 -2
  118. data/lib/active_scaffold/constraints.rb +39 -36
  119. data/lib/active_scaffold/core.rb +36 -15
  120. data/lib/active_scaffold/data_structures/action_columns.rb +14 -9
  121. data/lib/active_scaffold/data_structures/action_link.rb +4 -5
  122. data/lib/active_scaffold/data_structures/action_links.rb +5 -4
  123. data/lib/active_scaffold/data_structures/actions.rb +2 -2
  124. data/lib/active_scaffold/data_structures/association.rb +8 -0
  125. data/lib/active_scaffold/data_structures/association/abstract.rb +147 -0
  126. data/lib/active_scaffold/data_structures/association/active_mongoid.rb +42 -0
  127. data/lib/active_scaffold/data_structures/association/active_record.rb +94 -0
  128. data/lib/active_scaffold/data_structures/association/mongoid.rb +45 -0
  129. data/lib/active_scaffold/data_structures/bridge.rb +3 -6
  130. data/lib/active_scaffold/data_structures/column.rb +100 -82
  131. data/lib/active_scaffold/data_structures/columns.rb +21 -3
  132. data/lib/active_scaffold/data_structures/nested_info.rb +22 -37
  133. data/lib/active_scaffold/data_structures/set.rb +4 -4
  134. data/lib/active_scaffold/data_structures/sorting.rb +29 -15
  135. data/lib/active_scaffold/engine.rb +3 -1
  136. data/lib/active_scaffold/extensions/action_controller_rendering.rb +10 -5
  137. data/lib/active_scaffold/extensions/action_view_rendering.rb +65 -59
  138. data/lib/active_scaffold/extensions/left_outer_joins.rb +48 -53
  139. data/lib/active_scaffold/extensions/localize.rb +3 -4
  140. data/lib/active_scaffold/extensions/name_option_for_datetime.rb +7 -11
  141. data/lib/active_scaffold/extensions/paginator_extensions.rb +20 -18
  142. data/lib/active_scaffold/extensions/routing_mapper.rb +104 -40
  143. data/lib/active_scaffold/extensions/to_label.rb +1 -1
  144. data/lib/active_scaffold/extensions/unsaved_associated.rb +4 -13
  145. data/lib/active_scaffold/extensions/unsaved_record.rb +12 -1
  146. data/lib/active_scaffold/finder.rb +200 -134
  147. data/lib/active_scaffold/helpers/action_link_helpers.rb +398 -0
  148. data/lib/active_scaffold/helpers/association_helpers.rb +12 -30
  149. data/lib/active_scaffold/helpers/controller_helpers.rb +74 -24
  150. data/lib/active_scaffold/helpers/form_column_helpers.rb +205 -112
  151. data/lib/active_scaffold/helpers/human_condition_helpers.rb +21 -11
  152. data/lib/active_scaffold/helpers/id_helpers.rb +1 -1
  153. data/lib/active_scaffold/helpers/list_column_helpers.rb +117 -39
  154. data/lib/active_scaffold/helpers/pagination_helpers.rb +11 -14
  155. data/lib/active_scaffold/helpers/search_column_helpers.rb +69 -32
  156. data/lib/active_scaffold/helpers/show_column_helpers.rb +9 -3
  157. data/lib/active_scaffold/helpers/view_helpers.rb +41 -426
  158. data/lib/active_scaffold/orm_checks.rb +109 -0
  159. data/lib/active_scaffold/paginator.rb +1 -1
  160. data/lib/active_scaffold/responds_to_parent.rb +12 -10
  161. data/lib/active_scaffold/tableless.rb +81 -43
  162. data/lib/active_scaffold/version.rb +2 -2
  163. data/lib/generators/active_scaffold/controller_generator.rb +49 -0
  164. data/lib/generators/active_scaffold/install_generator.rb +45 -0
  165. data/lib/generators/active_scaffold/resource_generator.rb +56 -0
  166. data/lib/generators/{active_scaffold_controller/templates → templates}/controller.rb +0 -0
  167. data/lib/generators/{active_scaffold_controller/templates → templates}/helper.rb +0 -0
  168. data/shoulda_macros/macros.rb +3 -3
  169. data/test/active_scaffold_config_mock.rb +33 -0
  170. data/test/bridges/bridge_test.rb +9 -9
  171. data/test/bridges/date_picker_test.rb +3 -1
  172. data/test/bridges/paper_trail_test.rb +2 -3
  173. data/test/bridges/paperclip_test.rb +21 -10
  174. data/test/bridges/tiny_mce_test.rb +20 -21
  175. data/test/class_with_finder.rb +42 -0
  176. data/test/company.rb +6 -4
  177. data/test/config/core_test.rb +1 -1
  178. data/test/config/create_test.rb +1 -1
  179. data/test/config/list_test.rb +3 -3
  180. data/test/config/update_test.rb +3 -3
  181. data/test/data_structures/action_columns_test.rb +3 -3
  182. data/test/data_structures/association_column_test.rb +5 -5
  183. data/test/data_structures/column_test.rb +14 -14
  184. data/test/data_structures/columns_test.rb +2 -2
  185. data/test/data_structures/set_test.rb +2 -2
  186. data/test/data_structures/sorting_test.rb +6 -4
  187. data/test/extensions/active_record_test.rb +1 -1
  188. data/test/extensions/routing_mapper_test.rb +64 -13
  189. data/test/helpers/form_column_helpers_test.rb +6 -6
  190. data/test/helpers/list_column_helpers_test.rb +9 -5
  191. data/test/helpers/pagination_helpers_test.rb +1 -0
  192. data/test/misc/active_record_permissions_test.rb +18 -1
  193. data/test/misc/attribute_params_test.rb +26 -17
  194. data/test/misc/calculation_test.rb +8 -31
  195. data/test/misc/configurable_test.rb +3 -2
  196. data/test/misc/constraints_test.rb +33 -22
  197. data/test/misc/convert_numbers_format_test.rb +28 -10
  198. data/test/misc/finder_test.rb +6 -29
  199. data/test/misc/parse_datetime_test.rb +160 -0
  200. data/test/misc/render_test.rb +1 -1
  201. data/test/misc/tableless_test.rb +24 -0
  202. data/test/mock_app/app/models/building.rb +2 -1
  203. data/test/mock_app/config.ru +1 -1
  204. data/test/mock_app/config/environments/test.rb +1 -1
  205. data/test/mock_app/config/routes.rb +11 -3
  206. data/test/model_stub.rb +11 -6
  207. data/test/run_all.rb +1 -1
  208. data/test/test_helper.rb +19 -4
  209. metadata +42 -23
  210. data/lib/active_scaffold/data_structures/error_message.rb +0 -22
  211. data/lib/active_scaffold/extensions/reverse_associations.rb +0 -119
  212. data/lib/generators/active_scaffold/USAGE +0 -29
  213. data/lib/generators/active_scaffold/active_scaffold_generator.rb +0 -21
  214. data/lib/generators/active_scaffold_controller/USAGE +0 -19
  215. data/lib/generators/active_scaffold_controller/active_scaffold_controller_generator.rb +0 -29
  216. data/test/data_structures/error_message_test.rb +0 -25
@@ -1,73 +1,68 @@
1
- module ActiveScaffold
2
- module OuterJoins
3
- extend ActiveSupport::Concern
4
- attr_accessor :outer_joins_values
1
+ if Rails.version < '5.0.0'
2
+ module ActiveScaffold
3
+ module OuterJoins
4
+ extend ActiveSupport::Concern
5
5
 
6
- if Rails::VERSION::MAJOR < 4
7
- included do
8
- const_get(:MULTI_VALUE_METHODS) << :outer_joins
6
+ def left_outer_joins_values
7
+ @values[:left_outer_joins] || []
9
8
  end
10
9
 
11
- def outer_joins(*args)
12
- return self if args.compact.blank?
13
-
14
- relation = clone
15
-
16
- args.flatten!
17
- relation.joins_values += [''] # HACK: for using outer_joins in update_all/delete_all
18
- relation.outer_joins_values += args
19
-
20
- relation
21
- end
22
- else
23
- def outer_joins_values
24
- @values[:outer_joins] || []
10
+ def left_outer_joins_values=(values)
11
+ raise ImmutableRelation if @loaded
12
+ @values[:left_outer_joins] = values
25
13
  end
26
14
 
27
- def outer_joins_values=(values)
28
- raise ImmutableRelation if @loaded
29
- @values[:outer_joins] = values
15
+ def left_outer_joins(*args)
16
+ check_if_method_has_arguments!('left_outer_joins', args)
17
+ spawn.left_outer_joins!(*args.compact.flatten)
30
18
  end
19
+ alias left_joins left_outer_joins
31
20
 
32
21
  def outer_joins(*args)
33
- check_if_method_has_arguments!('outer_joins', args)
34
- spawn.outer_joins!(*args.compact.flatten)
22
+ ActiveSupport::Deprecation.warn 'use left_outer_joins or left_joins which is added to Rails 5.0.0'
23
+ left_outer_joins(*args)
35
24
  end
36
25
 
37
- def outer_joins!(*args)
38
- self.joins_values += [''] # HACK: for using outer_joins in update_all/delete_all
39
- self.outer_joins_values += args
26
+ def left_outer_joins!(*args)
27
+ self.joins_values += [''] # HACK: for using left_outer_joins in update_all/delete_all
28
+ self.left_outer_joins_values += args
40
29
  self
41
30
  end
42
- end
31
+ alias left_joins! left_outer_joins!
32
+
33
+ def outer_joins!(*args)
34
+ ActiveSupport::Deprecation.warn 'use left_outer_joins! or left_joins! which is added to Rails 5.0.0'
35
+ left_outer_joins!(*args)
36
+ end
43
37
 
44
- if Rails.version < '4.1'
45
- def build_arel
46
- if outer_joins_values.empty?
47
- super
48
- else
49
- relation = except(:outer_joins)
50
- join_dependency = ActiveRecord::Associations::JoinDependency.new(@klass, outer_joins_values, [])
51
- join_dependency.join_associations.each do |association|
52
- relation = association.join_relation(relation)
38
+ if Rails.version < '4.1'
39
+ def build_arel
40
+ if left_outer_joins_values.empty?
41
+ super
42
+ else
43
+ relation = except(:left_outer_joins)
44
+ join_dependency = ActiveRecord::Associations::JoinDependency.new(@klass, left_outer_joins_values, [])
45
+ join_dependency.join_associations.each do |association|
46
+ relation = association.join_relation(relation)
47
+ end
48
+ relation.build_arel
53
49
  end
54
- relation.build_arel
55
50
  end
56
- end
57
- else
58
- def build_arel
59
- if outer_joins_values.empty?
60
- super
61
- else
62
- relation = except(:outer_joins)
63
- relation.joins! ActiveRecord::Associations::JoinDependency.new(@klass, outer_joins_values, [])
64
- relation.build_arel
51
+ else
52
+ def build_arel
53
+ if left_outer_joins_values.empty?
54
+ super
55
+ else
56
+ relation = except(:left_outer_joins)
57
+ relation.joins! ActiveRecord::Associations::JoinDependency.new(@klass, left_outer_joins_values, [])
58
+ relation.build_arel
59
+ end
65
60
  end
66
61
  end
67
62
  end
68
63
  end
69
- end
70
- ActiveRecord::Relation.send :include, ActiveScaffold::OuterJoins
71
- module ActiveRecord::Querying
72
- delegate :outer_joins, :to => Rails::VERSION::MAJOR < 4 ? :scoped : :all
64
+ ActiveRecord::Relation.send :include, ActiveScaffold::OuterJoins
65
+ module ActiveRecord::Querying
66
+ delegate :left_outer_joins, :left_joins, :outer_joins, :to => :all
67
+ end
73
68
  end
@@ -1,10 +1,9 @@
1
1
  class Object
2
2
  def as_(key, options = {})
3
- unless key.blank?
4
- text = I18n.translate("#{key}", {:scope => [:active_scaffold, *options.delete(:scope)], :default => key.is_a?(String) ? key : key.to_s.titleize}.merge(options)).html_safe
3
+ if key.present?
4
+ text = I18n.translate(key.to_s, {:scope => [:active_scaffold, *options.delete(:scope)], :default => key.is_a?(String) ? key : key.to_s.titleize}.merge(options)).html_safe
5
5
  # text = nil if text.include?('translation missing:')
6
6
  end
7
- text ||= key
8
- text
7
+ text || key
9
8
  end
10
9
  end
@@ -1,17 +1,13 @@
1
1
  module ActiveScaffold
2
2
  module DateSelectExtension
3
- def datetime_selector_with_name(options, html_options)
4
- options.merge!(:prefix => options[:name].gsub(/\[[^\[]*\]$/, '')) if options[:name]
5
- datetime_selector_without_name(options, html_options)
6
- end
7
- def self.included(base)
8
- base.class_eval do
9
- alias_method_chain :datetime_selector, :name
10
- private :datetime_selector_without_name, :datetime_selector_with_name, :datetime_selector
11
- end
3
+ private
4
+
5
+ def datetime_selector(options, html_options)
6
+ options[:prefix] = options[:name].gsub(/\[[^\[]*\]$/, '') if options[:name]
7
+ super(options, html_options)
12
8
  end
13
9
  end
14
10
  end
15
- (defined?(ActionView::Helpers::Tags::DateSelect) ? ActionView::Helpers::Tags::DateSelect : ActionView::Helpers::InstanceTag).class_eval do
16
- include ActiveScaffold::DateSelectExtension
11
+ ActionView::Helpers::Tags::DateSelect.class_eval do
12
+ prepend ActiveScaffold::DateSelectExtension
17
13
  end
@@ -1,35 +1,37 @@
1
1
  require 'active_scaffold/paginator'
2
2
 
3
- class Paginator
4
- # Total number of pages
5
- def number_of_pages_with_infinite
6
- number_of_pages_without_infinite if @count
7
- end
8
- alias_method_chain :number_of_pages, :infinite
3
+ module ActiveScaffold
4
+ module Paginator
5
+ # Total number of pages
6
+ def number_of_pages
7
+ super if @count
8
+ end
9
9
 
10
- # Is this an "infinite" paginator
11
- def infinite?
12
- @count.nil?
13
- end
10
+ # Is this an "infinite" paginator
11
+ def infinite?
12
+ @count.nil?
13
+ end
14
14
 
15
- def count
16
- @count || first.items.size
15
+ def count
16
+ @count || first.items.size
17
+ end
17
18
  end
18
19
 
19
- class Page
20
+ module Page
20
21
  # Checks to see if there's a page after this one
21
- def next_with_infinite?
22
- return true if @pager.infinite?
23
- next_without_infinite?
22
+ def next?
23
+ @pager.infinite? || super
24
24
  end
25
- alias_method_chain :next?, :infinite
26
25
 
27
26
  def empty?
28
27
  if @pager.infinite?
29
28
  items.to_a.empty?
30
29
  else
31
- @pager.count == 0
30
+ @pager.count.zero?
32
31
  end
33
32
  end
34
33
  end
35
34
  end
35
+
36
+ Paginator.class_eval { prepend ActiveScaffold::Paginator }
37
+ Paginator::Page.class_eval { prepend ActiveScaffold::Page }
@@ -1,73 +1,137 @@
1
- module ActionDispatch
1
+ module ActiveScaffold
2
2
  module Routing
3
3
  ACTIVE_SCAFFOLD_CORE_ROUTING = {
4
4
  :collection => {:show_search => :get, :render_field => :post, :mark => :post},
5
- :member => {:update_column => :post, :render_field => [:get, :post], :mark => :post}
6
- }
5
+ :member => {:update_column => :post, :render_field => %i[get post], :mark => :post}
6
+ }.freeze
7
7
  ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING = {
8
8
  :collection => {:edit_associated => :get, :new_existing => :get, :add_existing => :post},
9
9
  :member => {:edit_associated => :get, :destroy_existing => :delete}
10
- }
10
+ }.freeze
11
+
12
+ class Association
13
+ def default_actions(actions_hash)
14
+ (actions_hash[:collection].keys + actions_hash[:member].keys).uniq
15
+ end
16
+
17
+ def get_actions(actions_hash, options)
18
+ default_actions = default_actions(actions_hash)
19
+ if only = options[:only]
20
+ Array(only).map(&:to_sym)
21
+ elsif except = options[:except]
22
+ default_actions - Array(except).map(&:to_sym)
23
+ else
24
+ default_actions
25
+ end
26
+ end
27
+
28
+ def call(mapper, options = {})
29
+ actions = get_actions(ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING, options)
30
+
31
+ mapper.collection do
32
+ ActionDispatch::Routing::ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING[:collection].each do |name, type|
33
+ mapper.match(name, via: type) if actions.include? name
34
+ end
35
+ end
36
+
37
+ mapper.member do
38
+ ActionDispatch::Routing::ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING[:member].each do |name, type|
39
+ mapper.match(name, via: type) if actions.include? name
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class Basic < Association
46
+ def initialize(defaults = {})
47
+ @defaults = defaults
48
+ end
49
+
50
+ def call(mapper, options = {})
51
+ options = @defaults.merge(options)
52
+ actions = get_actions(ACTIVE_SCAFFOLD_CORE_ROUTING, options)
53
+
54
+ mapper.collection do
55
+ ActionDispatch::Routing::ACTIVE_SCAFFOLD_CORE_ROUTING[:collection].each do |name, type|
56
+ mapper.match(name, via: type) if actions.include? name
57
+ end
58
+ end
59
+
60
+ mapper.member do
61
+ ActionDispatch::Routing::ACTIVE_SCAFFOLD_CORE_ROUTING[:member].each do |name, type|
62
+ mapper.match(name, via: type) if actions.include? name
63
+ end
64
+ mapper.get 'list', action: :index if mapper.send(:parent_resource).actions.include? :index
65
+ end
66
+
67
+ super if options[:association]
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ module ActionDispatch
74
+ module Routing
75
+ ACTIVE_SCAFFOLD_CORE_ROUTING = ActiveScaffold::Routing::ACTIVE_SCAFFOLD_CORE_ROUTING
76
+ ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING = ActiveScaffold::Routing::ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING
11
77
 
12
78
  class Mapper
13
79
  module Resources
14
- class ActiveScaffold < Resource
15
- def default_actions
16
- @default_actions ||= (ACTIVE_SCAFFOLD_CORE_ROUTING[:collection].keys + ACTIVE_SCAFFOLD_CORE_ROUTING[:member].keys).uniq
80
+ def parent_options
81
+ opts = parent_resource.instance_variable_get(:@options)
82
+ if Rails.version >= '5.0.0'
83
+ opts.merge(
84
+ only: parent_resource.instance_variable_get(:@only),
85
+ except: parent_resource.instance_variable_get(:@except)
86
+ )
17
87
  end
88
+ opts
18
89
  end
19
- class ActiveScaffoldAssociation < Resource
20
- def default_actions
21
- @default_actions ||= (ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING[:collection].keys + ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING[:member].keys).uniq
22
- end
90
+
91
+ def define_active_scaffold_concern
92
+ ActiveSupport::Deprecation.warn 'Add concern :active_scaffold, ActiveScaffold::Routing::Basic.new(association: true) to your routes file.'
93
+ concern :active_scaffold, ActiveScaffold::Routing::Basic.new(association: true)
23
94
  end
24
95
 
25
- def as_routes(opts = {:association => true})
26
- resource = parent_resource
27
- resource_scope(:resource, ActiveScaffold.new(parent_resource.name, parent_resource.options)) do
28
- collection do
29
- ActionDispatch::Routing::ACTIVE_SCAFFOLD_CORE_ROUTING[:collection].each do |name, type|
30
- match(name, :via => type) if parent_resource.actions.include? name
31
- end
32
- end
33
- member do
34
- ActionDispatch::Routing::ACTIVE_SCAFFOLD_CORE_ROUTING[:member].each do |name, type|
35
- match(name, :via => type) if parent_resource.actions.include? name
36
- end
37
- get 'list', :action => :index if resource.actions.include? :index
38
- end
96
+ def define_active_scaffold_association_concern
97
+ ActiveSupport::Deprecation.warn 'Add concern :active_scaffold_association, ActiveScaffold::Routing::Association.new to your routes file.'
98
+ concern :active_scaffold_association, ActiveScaffold::Routing::Association.new
99
+ end
100
+
101
+ def as_routes(opts = {association: true})
102
+ define_active_scaffold_concern unless @concerns[:active_scaffold]
103
+ if opts[:association] && !@concerns[:active_scaffold_association]
104
+ define_active_scaffold_association_concern
39
105
  end
40
- as_association_routes if opts[:association]
106
+ ActiveSupport::Deprecation.warn 'Use concerns: :active_scaffold in resources instead of as_routes, or concerns :active_scaffold in resources block if want to disable association routes or restrict routes with only or except options.'
107
+ concerns :active_scaffold, parent_options.merge(association: opts[:association])
41
108
  end
42
109
 
43
110
  def as_association_routes
44
- resource_scope(:resource, ActiveScaffoldAssociation.new(parent_resource.name, parent_resource.options)) do
45
- collection do
46
- ActionDispatch::Routing::ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING[:collection].each do |name, type|
47
- match(name, :via => type) if parent_resource.actions.include? name
48
- end
49
- end
50
- member do
51
- ActionDispatch::Routing::ACTIVE_SCAFFOLD_ASSOCIATION_ROUTING[:member].each do |name, type|
52
- match(name, :via => type) if parent_resource.actions.include? name
53
- end
54
- end
55
- end
111
+ define_active_scaffold_association_concern unless @concerns[:active_scaffold_association]
112
+ ActiveSupport::Deprecation.warn 'Use concerns: :active_scaffold_association in resources instead of as_association_routes, or concerns :active_scaffold_association in resources block if want to restrict routes with only or except options.'
113
+ concerns :active_scaffold_association, parent_options
56
114
  end
57
115
 
58
116
  def as_nested_resources(*resources)
59
117
  options = resources.extract_options!
118
+ nested_options = options.merge(parent_scaffold: parent_scaffold)
60
119
  resources.each do |resource|
61
- resources(resource, options.merge(:parent_scaffold => merge_module_scope(@scope[:module], parent_resource.plural), :association => resource)) { yield if block_given? }
120
+ resources(resource, nested_options.merge(association: resource)) { yield if block_given? }
62
121
  end
63
122
  end
64
123
 
65
124
  def as_scoped_routes(*scopes)
66
125
  options = scopes.extract_options!
126
+ scoped_options = options.merge(parent_scaffold: parent_scaffold, association: parent_resource.plural)
67
127
  scopes.each do |scope|
68
- resources(scope, options.merge(:parent_scaffold => merge_module_scope(@scope[:module], parent_resource.plural), :named_scope => scope, :controller => parent_resource.plural)) { yield if block_given? }
128
+ resources(scope, scoped_options.merge(named_scope: scope)) { yield if block_given? }
69
129
  end
70
130
  end
131
+
132
+ def parent_scaffold
133
+ merge_module_scope(@scope[:module], parent_resource.plural)
134
+ end
71
135
  end
72
136
  end
73
137
  end
@@ -1,7 +1,7 @@
1
1
  # the ever-useful to_label method
2
2
  class ActiveRecord::Base
3
3
  def to_label
4
- [:name, :label, :title, :to_s].each do |attribute|
4
+ %i[name label title to_s].each do |attribute|
5
5
  return send(attribute).to_s if respond_to?(attribute)
6
6
  end
7
7
  end
@@ -13,20 +13,11 @@ class ActiveRecord::Base
13
13
  end
14
14
 
15
15
  def save_associated!
16
- save_associated || raise(ActiveRecord::RecordNotSaved)
16
+ save_associated || raise(ActiveRecord::RecordNotSaved, "Fail saving associations for #{inspect}")
17
17
  end
18
18
 
19
19
  def no_errors_in_associated?
20
- with_unsaved_associated { |a| a.errors.count == 0 && a.no_errors_in_associated? }
21
- end
22
-
23
- def keeping_errors
24
- old_errors = errors.dup if errors.present?
25
- yield.tap do
26
- (old_errors || []).each do |attr|
27
- old_errors[attr].each { |msg| errors.add(attr, msg) unless errors.added?(attr, msg) }
28
- end
29
- end
20
+ with_unsaved_associated { |a| a.errors.count.zero? && a.no_errors_in_associated? }
30
21
  end
31
22
 
32
23
  protected
@@ -42,9 +33,9 @@ class ActiveRecord::Base
42
33
  #
43
34
  # Otherwise the default behaviour of traversing all associations will be preserved.
44
35
  def associations_for_update
45
- if self.respond_to?(:scaffold_update_nofollow)
36
+ if respond_to?(:scaffold_update_nofollow)
46
37
  self.class.reflect_on_all_associations.reject { |association| scaffold_update_nofollow.include?(association.name) }
47
- elsif self.respond_to?(:scaffold_update_follow)
38
+ elsif respond_to?(:scaffold_update_follow)
48
39
  self.class.reflect_on_all_associations.select { |association| scaffold_update_follow.include?(association.name) }
49
40
  else
50
41
  self.class.reflect_on_all_associations
@@ -2,7 +2,7 @@
2
2
  module ActiveScaffold::UnsavedRecord
3
3
  # acts like a dirty? flag, manually thrown during update_record_from_params.
4
4
  def unsaved=(val)
5
- @unsaved = (val) ? true : false
5
+ @unsaved = val ? true : false
6
6
  end
7
7
 
8
8
  # whether the unsaved? flag has been thrown
@@ -14,5 +14,16 @@ module ActiveScaffold::UnsavedRecord
14
14
  def save(*)
15
15
  super.tap { self.unsaved = false }
16
16
  end
17
+
18
+ def keeping_errors
19
+ old_errors = errors.dup if errors.present?
20
+ result = yield
21
+ if old_errors
22
+ old_errors.each do |attr|
23
+ old_errors[attr].each { |msg| errors.add(attr, msg) unless errors.added?(attr, msg) }
24
+ end
25
+ end
26
+ result && old_errors.blank?
27
+ end
17
28
  end
18
29
  ActiveRecord::Base.class_eval { include ActiveScaffold::UnsavedRecord }