active_scaffold 3.1.20 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. data/app/assets/javascripts/jquery/active_scaffold.js +4 -1
  2. data/app/assets/javascripts/prototype/active_scaffold.js +4 -1
  3. data/app/assets/stylesheets/{active_scaffold.scss~ → active_scaffold.css.scss} +5 -2
  4. data/app/assets/stylesheets/{active_scaffold_colors.scss → active_scaffold_colors.css.scss} +4 -0
  5. data/app/assets/stylesheets/active_scaffold_extensions.css.erb +2 -0
  6. data/app/assets/stylesheets/active_scaffold_images.css.scss +43 -0
  7. data/lib/active_scaffold/actions/core.rb +1 -0
  8. data/lib/active_scaffold/actions/nested.rb +0 -1
  9. data/lib/active_scaffold/extensions/action_view_rendering.rb +1 -1
  10. data/lib/active_scaffold/helpers/form_column_helpers.rb +1 -0
  11. data/lib/active_scaffold/version.rb +2 -2
  12. data/test/mock_app/.gitignore +2 -0
  13. metadata +86 -164
  14. data/app/assets/javascripts/active_scaffold.js.erb~ +0 -16
  15. data/app/assets/javascripts/jquery/active_scaffold.js~ +0 -1053
  16. data/app/assets/javascripts/jquery/draggable_lists.js~ +0 -27
  17. data/app/assets/javascripts/prototype/active_scaffold.js~ +0 -1037
  18. data/app/assets/stylesheets/active_scaffold.css.erb +0 -11
  19. data/app/assets/stylesheets/active_scaffold.css.erb~ +0 -11
  20. data/app/assets/stylesheets/active_scaffold.css.scss.erb~ +0 -1120
  21. data/app/assets/stylesheets/active_scaffold.css.scss~ +0 -11
  22. data/app/assets/stylesheets/active_scaffold.css~ +0 -11
  23. data/app/assets/stylesheets/active_scaffold_colors.css +0 -244
  24. data/app/assets/stylesheets/active_scaffold_colors.css.scss~ +0 -481
  25. data/app/assets/stylesheets/active_scaffold_default.css.erb +0 -47
  26. data/app/assets/stylesheets/active_scaffold_default.css.erb~ +0 -57
  27. data/app/assets/stylesheets/active_scaffold_default.css.scss~ +0 -1092
  28. data/app/assets/stylesheets/active_scaffold_default.css~ +0 -923
  29. data/app/assets/stylesheets/active_scaffold_layout.css~ +0 -922
  30. data/app/assets/stylesheets/blue-theme.css~ +0 -1150
  31. data/config/locales/es.yml~ +0 -120
  32. data/frontends/default/views/_action_group.html.erb~ +0 -24
  33. data/frontends/default/views/_base_form.html.erb~ +0 -42
  34. data/frontends/default/views/_form.html.erb~ +0 -26
  35. data/frontends/default/views/_form_association.html.erb~ +0 -19
  36. data/frontends/default/views/_form_association_footer.html.erb~ +0 -48
  37. data/frontends/default/views/_horizontal_subform.html.erb~ +0 -32
  38. data/frontends/default/views/_horizontal_subform_header.html.erb~ +0 -11
  39. data/frontends/default/views/_horizontal_subform_record.html.erb~ +0 -38
  40. data/frontends/default/views/_list_actions.html.erb~ +0 -15
  41. data/frontends/default/views/_list_inline_adapter.html.erb~ +0 -10
  42. data/frontends/default/views/_list_messages.html.erb~ +0 -30
  43. data/frontends/default/views/_list_pagination.html.erb~ +0 -11
  44. data/frontends/default/views/_list_pagination_links.html.erb~ +0 -0
  45. data/frontends/default/views/_list_with_header.html.erb~ +0 -36
  46. data/frontends/default/views/_render_field.js.erb~ +0 -23
  47. data/frontends/default/views/_row.html.erb~ +0 -6
  48. data/frontends/default/views/_show.html.erb~ +0 -8
  49. data/frontends/default/views/_update_form.html.erb~ +0 -6
  50. data/frontends/default/views/_vertical_subform.html.erb~ +0 -12
  51. data/frontends/default/views/add_existing.js.erb~ +0 -18
  52. data/frontends/default/views/add_existing_form.html.erb~ +0 -5
  53. data/frontends/default/views/create.html.erb~ +0 -5
  54. data/frontends/default/views/edit_associated.js.erb~ +0 -13
  55. data/frontends/default/views/on_create.js.erb~ +0 -45
  56. data/frontends/default/views/on_update.js.erb~ +0 -31
  57. data/frontends/default/views/render_field.js.erb~ +0 -1
  58. data/frontends/default/views/search.html.erb~ +0 -5
  59. data/frontends/default/views/show.html.erb~ +0 -5
  60. data/frontends/default/views/update.html.erb~ +0 -8
  61. data/frontends/default/views/update_column.js.erb~ +0 -16
  62. data/lib/active_scaffold.rb~ +0 -373
  63. data/lib/active_scaffold/actions/core.rb~ +0 -203
  64. data/lib/active_scaffold/actions/create.rb~ +0 -148
  65. data/lib/active_scaffold/actions/list.rb~ +0 -196
  66. data/lib/active_scaffold/actions/nested.rb~ +0 -247
  67. data/lib/active_scaffold/actions/search.rb~ +0 -49
  68. data/lib/active_scaffold/actions/subform.rb~ +0 -27
  69. data/lib/active_scaffold/actions/update.rb~ +0 -150
  70. data/lib/active_scaffold/attribute_params.rb~ +0 -203
  71. data/lib/active_scaffold/bridges/date_picker/helper.rb~ +0 -180
  72. data/lib/active_scaffold/bridges/record_select/helpers.rb~ +0 -86
  73. data/lib/active_scaffold/bridges/shared/date_bridge.rb~ +0 -209
  74. data/lib/active_scaffold/config/base.rb~ +0 -72
  75. data/lib/active_scaffold/config/list.rb~ +0 -195
  76. data/lib/active_scaffold/config/nested.rb~ +0 -41
  77. data/lib/active_scaffold/config/search.rb~ +0 -74
  78. data/lib/active_scaffold/constraints.rb~ +0 -186
  79. data/lib/active_scaffold/data_structures/action_columns.rb~ +0 -140
  80. data/lib/active_scaffold/data_structures/action_link.rb~ +0 -179
  81. data/lib/active_scaffold/data_structures/nested_info.rb~ +0 -147
  82. data/lib/active_scaffold/extensions/action_controller_rendering.rb~ +0 -22
  83. data/lib/active_scaffold/extensions/action_view_rendering.rb~ +0 -120
  84. data/lib/active_scaffold/extensions/active_association_reflection.rb~ +0 -22
  85. data/lib/active_scaffold/extensions/cache_association.rb~ +0 -12
  86. data/lib/active_scaffold/extensions/reverse_associations.rb~ +0 -64
  87. data/lib/active_scaffold/extensions/routing_mapper.rb~ +0 -48
  88. data/lib/active_scaffold/extensions/unsaved_associated.rb~ +0 -62
  89. data/lib/active_scaffold/finder.rb~ +0 -370
  90. data/lib/active_scaffold/helpers/controller_helpers.rb~ +0 -92
  91. data/lib/active_scaffold/helpers/form_column_helpers.rb~ +0 -320
  92. data/lib/active_scaffold/helpers/id_helpers.rb~ +0 -123
  93. data/lib/active_scaffold/helpers/list_column_helpers.rb~ +0 -370
  94. data/lib/active_scaffold/helpers/search_column_helpers.rb~ +0 -263
  95. data/lib/active_scaffold/helpers/view_helpers.rb~ +0 -350
@@ -1,203 +0,0 @@
1
- module ActiveScaffold
2
- # Provides support for param hashes assumed to be model attributes.
3
- # Support is primarily needed for creating/editing associated records using a nested hash structure.
4
- #
5
- # Paradigm Params Hash (should write unit tests on this):
6
- # params[:record] = {
7
- # # a simple record attribute
8
- # 'name' => 'John',
9
- # # a plural association hash
10
- # 'roles' => {
11
- # # associate with an existing role
12
- # '5' => {'id' => 5}
13
- # # associate with an existing role and edit it
14
- # '6' => {'id' => 6, 'name' => 'designer'}
15
- # # create and associate a new role
16
- # '124521' => {'name' => 'marketer'}
17
- # }
18
- # # a singular association hash
19
- # 'location' => {'id' => 12, 'city' => 'New York'}
20
- # }
21
- #
22
- # Simpler association structures are also supported, like:
23
- # params[:record] = {
24
- # # a simple record attribute
25
- # 'name' => 'John',
26
- # # a plural association ... all ids refer to existing records
27
- # 'roles' => ['5', '6'],
28
- # # a singular association ... all ids refer to existing records
29
- # 'location' => '12'
30
- # }
31
- module AttributeParams
32
- protected
33
- # Takes attributes (as from params[:record]) and applies them to the parent_record. Also looks for
34
- # association attributes and attempts to instantiate them as associated objects.
35
- #
36
- # This is a secure way to apply params to a record, because it's based on a loop over the columns
37
- # set. The columns set will not yield unauthorized columns, and it will not yield unregistered columns.
38
- def update_record_from_params(parent_record, columns, attributes)
39
- crud_type = parent_record.new_record? ? :create : :update
40
- return parent_record unless parent_record.authorized_for?(:crud_type => crud_type)
41
-
42
- multi_parameter_attributes = {}
43
- attributes.each do |k, v|
44
- next unless k.include? '('
45
- column_name = k.split('(').first.to_sym
46
- multi_parameter_attributes[column_name] ||= []
47
- multi_parameter_attributes[column_name] << [k, v]
48
- end
49
-
50
- columns.each :for => parent_record, :crud_type => crud_type, :flatten => true do |column|
51
- # Set any passthrough parameters that may be associated with this column (ie, file column "keep" and "temp" attributes)
52
- unless column.params.empty?
53
- column.params.each{|p| parent_record.send("#{p}=", attributes[p]) if attributes.has_key? p}
54
- end
55
-
56
- if multi_parameter_attributes.has_key? column.name
57
- parent_record.send(:assign_multiparameter_attributes, multi_parameter_attributes[column.name])
58
- elsif attributes.has_key? column.name
59
- value = column_value_from_param_value(parent_record, column, attributes[column.name])
60
-
61
- # we avoid assigning a value that already exists because otherwise has_one associations will break (AR bug in has_one_association.rb#replace)
62
- parent_record.send("#{column.name}=", value) unless parent_record.send(column.name) == value
63
-
64
- elsif column.plural_association?
65
- parent_record.send("#{column.name}=", [])
66
- end
67
- end
68
-
69
- if parent_record.new_record?
70
- parent_record.class.reflect_on_all_associations.each do |a|
71
- next unless [:has_one, :has_many].include?(a.macro) and not (a.options[:through] || a.options[:finder_sql])
72
- next unless association_proxy = parent_record.send(a.name)
73
-
74
- raise ActiveScaffold::ReverseAssociationRequired, "Association #{a.name} in class #{parent_record.class.name}: In order to support :has_one and :has_many where the parent record is new and the child record(s) validate the presence of the parent, ActiveScaffold requires the reverse association (the belongs_to)." unless a.reverse
75
-
76
- association_proxy = [association_proxy] if a.macro == :has_one
77
- association_proxy.each { |record| record.send("#{a.reverse}=", parent_record) }
78
- end
79
- end
80
-
81
- parent_record
82
- end
83
-
84
- def manage_nested_record_from_params(parent_record, column, attributes)
85
- record = find_or_create_for_params(attributes, column, parent_record)
86
- if record
87
- record_columns = active_scaffold_config_for(column.association.klass).subform.columns
88
- record_columns.constraint_columns = [column.association.reverse]
89
- debugger
90
- update_record_from_params(record, record_columns, attributes)
91
- record.unsaved = true
92
- end
93
- record
94
- end
95
-
96
- def column_value_from_param_value(parent_record, column, value)
97
- # convert the value, possibly by instantiating associated objects
98
- if value.is_a?(Hash)
99
- column_value_from_param_hash_value(parent_record, column, value)
100
- else
101
- column_value_from_param_simple_value(parent_record, column, value)
102
- end
103
- end
104
-
105
- def column_value_from_param_simple_value(parent_record, column, value)
106
- if column.singular_association?
107
- # it's a single id
108
- column.association.klass.find(value) if value and not value.empty?
109
- elsif column.plural_association?
110
- column_plural_assocation_value_from_value(column, value)
111
- elsif column.number? && [:i18n_number, :currency].include?(column.options[:format])
112
- self.class.i18n_number_to_native_format(value)
113
- else
114
- # convert empty strings into nil. this works better with 'null => true' columns (and validations),
115
- # and 'null => false' columns should just convert back to an empty string.
116
- # ... but we can at least check the ConnectionAdapter::Column object to see if nulls are allowed
117
- value = nil if value.is_a? String and value.empty? and !column.column.nil? and column.column.null
118
- value
119
- end
120
- end
121
-
122
- def column_plural_assocation_value_from_value(column, value)
123
- # it's an array of ids
124
- if value and not value.empty?
125
- ids = value.select {|id| id.respond_to?(:empty?) ? !id.empty? : true}
126
- ids.empty? ? [] : column.association.klass.find(ids)
127
- end
128
- end
129
-
130
- def column_value_from_param_hash_value(parent_record, column, value)
131
- # this is just for backwards compatibility. we should clean this up in 2.0.
132
- if column.form_ui == :select
133
- ids = if column.singular_association?
134
- value[:id]
135
- else
136
- value.values.collect {|hash| hash[:id]}
137
- end
138
- (ids and not ids.empty?) ? column.association.klass.find(ids) : nil
139
-
140
- elsif column.singular_association?
141
- manage_nested_record_from_params(parent_record, column, value)
142
- elsif column.plural_association?
143
- value.collect {|key_value_pair| manage_nested_record_from_params(parent_record, column, key_value_pair[1])}.compact
144
- else
145
- value
146
- end
147
- end
148
-
149
- # Attempts to create or find an instance of klass (which must be an ActiveRecord object) from the
150
- # request parameters given. If params[:id] exists it will attempt to find an existing object
151
- # otherwise it will build a new one.
152
- def find_or_create_for_params(params, parent_column, parent_record)
153
- current = parent_record.send(parent_column.name)
154
- klass = parent_column.association.klass
155
- pk = klass.primary_key.to_sym
156
- return nil if parent_column.show_blank_record?(current) and attributes_hash_is_empty?(params, klass)
157
-
158
- if params.has_key? pk
159
- # modifying the current object of a singular association
160
- pk_val = params[pk]
161
- if current and current.is_a? ActiveRecord::Base and current.id.to_s == pk_val
162
- current
163
- # modifying one of the current objects in a plural association
164
- elsif current and current.respond_to?(:any?) and current.any? {|o| o.id.to_s == pk_val}
165
- current.detect {|o| o.id.to_s == pk_val}
166
- # attaching an existing but not-current object
167
- else
168
- klass.find(pk_val)
169
- end
170
- else
171
- build_associated(parent_column, parent_record) if klass.authorized_for?(:crud_type => :create)
172
- end
173
- end
174
- # Determines whether the given attributes hash is "empty".
175
- # This isn't a literal emptiness - it's an attempt to discern whether the user intended it to be empty or not.
176
- def attributes_hash_is_empty?(hash, klass)
177
- ignore_column_types = [:boolean]
178
- hash.all? do |key,value|
179
- # convert any possible multi-parameter attributes like 'created_at(5i)' to simply 'created_at'
180
- parts = key.to_s.split('(')
181
- #old style date form management... ignore them too
182
- ignore_column_types = [:boolean, :datetime, :date, :time] if parts.length > 1
183
- column_name = parts.first
184
- column = klass.columns_hash[column_name]
185
-
186
- # booleans and datetimes will always have a value. so we ignore them when checking whether the hash is empty.
187
- # this could be a bad idea. but the current situation (excess record entry) seems worse.
188
- next true if column and ignore_column_types.include?(column.type)
189
-
190
- # defaults are pre-filled on the form. we can't use them to determine if the user intends a new row.
191
- next true if column and value == column.default.to_s
192
-
193
- if value.is_a?(Hash)
194
- attributes_hash_is_empty?(value, klass)
195
- elsif value.is_a?(Array)
196
- value.any? {|id| id.respond_to?(:empty?) ? !id.empty? : true}
197
- else
198
- value.respond_to?(:empty?) ? value.empty? : false
199
- end
200
- end
201
- end
202
- end
203
- end
@@ -1,180 +0,0 @@
1
- module ActiveScaffold::Bridges
2
- class DatePicker
3
- module Helper
4
- DATE_FORMAT_CONVERSION = {
5
- /%a/ => 'D',
6
- /%A/ => 'DD',
7
- /%b/ => 'M',
8
- /%B/ => 'MM',
9
- /%d/ => 'dd',
10
- /%e/ => 'd',
11
- /%j/ => 'oo',
12
- /%m/ => 'mm',
13
- /%y/ => 'y',
14
- /%Y/ => 'yy',
15
- /%H/ => 'hh', # options ampm => false
16
- /%I/ => 'hh', # options ampm => true
17
- /%M/ => 'mm',
18
- /%p/ => 'tt',
19
- /%S/ => 'ss',
20
- /%[cUWwxXZz]/ => ''
21
- }
22
-
23
- def self.date_options_for_locales
24
- I18n.available_locales.collect do |locale|
25
- locale_date_options = date_options(locale)
26
- if locale_date_options
27
- "$.datepicker.regional['#{locale}'] = #{locale_date_options.to_json};"
28
- else
29
- nil
30
- end
31
- end.compact.join('')
32
- end
33
-
34
- def self.date_options(locale)
35
- begin
36
- date_picker_options = { :closeText => as_(:close),
37
- :prevText => as_(:previous),
38
- :nextText => as_(:next),
39
- :currentText => as_(:today),
40
- :monthNames => I18n.translate!('date.month_names', :locale => locale)[1..-1],
41
- :monthNamesShort => I18n.translate!('date.abbr_month_names', :locale => locale)[1..-1],
42
- :dayNames => I18n.translate!('date.day_names', :locale => locale),
43
- :dayNamesShort => I18n.translate!('date.abbr_day_names', :locale => locale),
44
- :dayNamesMin => I18n.translate!('date.abbr_day_names', :locale => locale),
45
- :changeYear => true,
46
- :changeMonth => true,
47
- }
48
-
49
- as_date_picker_options = I18n.translate! :date_picker_options, :scope => :active_scaffold, :locale => locale, :default => ''
50
- date_picker_options.merge!(as_date_picker_options) if as_date_picker_options.is_a? Hash
51
- Rails.logger.warn "ActiveScaffold: Missing date picker localization for your locale: #{locale}" if as_date_picker_options.blank?
52
-
53
- js_format = self.to_datepicker_format(I18n.translate!('date.formats.default', :locale => locale, :default => ''))
54
- date_picker_options[:dateFormat] = js_format unless js_format.blank?
55
- date_picker_options
56
- rescue
57
- raise if locale == I18n.locale
58
- end
59
- end
60
-
61
- def self.datetime_options_for_locales
62
- I18n.available_locales.collect do |locale|
63
- locale_datetime_options = datetime_options(locale)
64
- if locale_datetime_options
65
- "$.timepicker.regional['#{locale}'] = #{locale_datetime_options.to_json};"
66
- else
67
- nil
68
- end
69
- end.compact.join('')
70
- end
71
-
72
- def self.datetime_options(locale)
73
- begin
74
- rails_time_format = I18n.translate! 'time.formats.picker', :locale => locale
75
- datetime_picker_options = {:ampm => false,
76
- :hourText => I18n.translate!('datetime.prompts.hour', :locale => locale),
77
- :minuteText => I18n.translate!('datetime.prompts.minute', :locale => locale),
78
- :secondText => I18n.translate!('datetime.prompts.second', :locale => locale)
79
- }
80
-
81
- as_datetime_picker_options = I18n.translate! :datetime_picker_options, :scope => :active_scaffold, :locale => locale, :default => ''
82
- datetime_picker_options.merge!(as_datetime_picker_options) if as_datetime_picker_options.is_a? Hash
83
- Rails.logger.warn "ActiveScaffold: Missing datetime picker localization for your locale: #{locale}" if as_datetime_picker_options.blank?
84
-
85
- date_format, time_format = self.split_datetime_format(self.to_datepicker_format(rails_time_format))
86
- datetime_picker_options[:dateFormat] = date_format unless date_format.nil?
87
- unless time_format.nil?
88
- datetime_picker_options[:timeFormat] = time_format
89
- datetime_picker_options[:ampm] = true if rails_time_format.include?('%I')
90
- end
91
- datetime_picker_options
92
- rescue
93
- raise if locale == I18n.locale
94
- end
95
- end
96
-
97
- def self.to_datepicker_format(rails_format)
98
- return nil if rails_format.nil?
99
- if rails_format =~ /%[cUWwxXZz]/
100
- Rails.logger.warn("AS DatePicker::Helper: rails date format #{rails_format} includes options which can't be converted to jquery datepicker format. Options %c, %U, %W, %w, %x %X, %z, %Z are not supported by datepicker and will be removed")
101
- nil
102
- end
103
- js_format = rails_format.dup
104
- DATE_FORMAT_CONVERSION.each do |key, value|
105
- js_format.gsub!(key, value)
106
- end
107
- js_format
108
- end
109
-
110
- def self.split_datetime_format(datetime_format)
111
- date_format = datetime_format
112
- time_format = nil
113
- time_start_indicators = %w{hh mm tt ss}
114
- unless datetime_format.nil?
115
- start_indicator = time_start_indicators.detect {|indicator| datetime_format.include?(indicator)}
116
- unless start_indicator.nil?
117
- pos_time_format = datetime_format.index(start_indicator)
118
- date_format = datetime_format.to(pos_time_format - 1)
119
- time_format = datetime_format.from(pos_time_format)
120
- end
121
- end
122
- return date_format, time_format
123
- end
124
-
125
- module DatepickerColumnHelpers
126
- def datepicker_split_datetime_format(datetime_format)
127
- ActiveScaffold::Bridges::DatePicker::Helper.split_datetime_format(datetime_format)
128
- end
129
-
130
- def to_datepicker_format(rails_format)
131
- ActiveScaffold::Bridges::DatePicker::Helper.to_datepicker_format(rails_format)
132
- end
133
-
134
- def datepicker_format_options(column, format, options)
135
- if column.form_ui == :date_picker
136
- js_format = to_datepicker_format(I18n.translate!("date.formats.#{format}"))
137
- options['date:dateFormat'] = js_format unless js_format.nil?
138
- else
139
- rails_time_format = I18n.translate!("time.formats.#{format}")
140
- date_format, time_format = datepicker_split_datetime_format(self.to_datepicker_format(rails_time_format))
141
- options['date:dateFormat'] = date_format unless date_format.nil?
142
- unless time_format.nil?
143
- options['time:timeFormat'] = time_format
144
- options['time:ampm'] = true if rails_time_format.include?('%I')
145
- end
146
- end unless format == :default
147
- end
148
- end
149
-
150
- module SearchColumnHelpers
151
- def active_scaffold_search_date_bridge_calendar_control(column, options, current_search, name)
152
- if current_search.is_a? Hash
153
- value = controller.class.condition_value_for_datetime(current_search[name], column.form_ui == :date_picker ? :to_date : :to_time)
154
- else
155
- value = current_search
156
- end
157
- options = column.options.merge(options).except!(:include_blank, :discard_time, :discard_date, :value)
158
- options = active_scaffold_input_text_options(options.merge(column.options))
159
- options[:class] << " #{column.search_ui.to_s}"
160
- options[:style] = "display:#{(options[:show].nil? || options[:show]) ? '' : 'none'}"
161
- format = options.delete(:format) || :default
162
- datepicker_format_options(column, format, options)
163
- text_field_tag("#{options[:name]}[#{name}]", value ? l(value, :format => format) : nil, options.merge(:id => "#{options[:id]}_#{name}", :name => "#{options[:name]}[#{name}]"))
164
- end
165
- end
166
-
167
- module FormColumnHelpers
168
- def active_scaffold_input_date_picker(column, options)
169
- options = active_scaffold_input_text_options(options.merge(column.options))
170
- options[:class] << " #{column.form_ui.to_s}"
171
- value = controller.class.condition_value_for_datetime(@record.send(column.name), column.form_ui == :date_picker ? :to_date : :to_time)
172
- format = options.delete(:format) || :default
173
- datepicker_format_options(column, format, options)
174
- options[:value] = (value ? l(value, :format => format) : nil)
175
- text_field(:record, column.name, options)
176
- end
177
- end
178
- end
179
- end
180
- end
@@ -1,86 +0,0 @@
1
- class ActiveScaffold::Bridges::RecordSelect
2
- module Helpers
3
- def self.included(base)
4
- base.class_eval do
5
- include FormColumnHelpers
6
- include SearchColumnHelpers
7
- end
8
- end
9
-
10
- module FormColumnHelpers
11
- # requires RecordSelect plugin to be installed and configured.
12
- def active_scaffold_input_record_select(column, options)
13
- if column.singular_association?
14
- multiple = false
15
- multiple = column.options[:html_options][:multiple] if column.options[:html_options] && column.options[:html_options][:multiple]
16
- active_scaffold_record_select(column, options, @record.send(column.name), multiple)
17
- elsif column.plural_association?
18
- active_scaffold_record_select(column, options, @record.send(column.name), true)
19
- else
20
- active_scaffold_record_select_autocomplete(column, options)
21
- end
22
- end
23
-
24
- def active_scaffold_record_select(column, options, value, multiple)
25
- unless column.association
26
- raise ArgumentError, "record_select can only work against associations (and #{column.name} is not). A common mistake is to specify the foreign key field (like :user_id), instead of the association (:user)."
27
- end
28
- remote_controller = active_scaffold_controller_for(column.association.klass).controller_path
29
-
30
- # if the opposite association is a :belongs_to (in that case association in this class must be has_one or has_many)
31
- # then only show records that have not been associated yet
32
- if [:has_one, :has_many].include?(column.association.macro)
33
- params.merge!({column.association.primary_key_name => ''})
34
- end
35
-
36
- record_select_options = active_scaffold_input_text_options(options).merge(
37
- :controller => remote_controller
38
- )
39
- record_select_options.merge!(column.options)
40
-
41
- html = if multiple
42
- record_multi_select_field(options[:name], value || [], record_select_options)
43
- else
44
- record_select_field(options[:name], value || column.association.klass.new, record_select_options)
45
- end
46
- html = self.class.field_error_proc.call(html, self) if @record.errors[column.name].any?
47
- html
48
- end
49
-
50
- def active_scaffold_record_select_autocomplete(column, options)
51
- remote_controller = active_scaffold_controller_for(column.association.klass).controller_path
52
- record_select_options = active_scaffold_input_text_options(options).merge(
53
- :controller => remote_controller
54
- )
55
- html = record_select_autocomplete(options[:name], @record, record_select_options)
56
- html = self.class.field_error_proc.call(html, self) if @record.errors[column.name].any?
57
- html
58
- end
59
- end
60
-
61
- module SearchColumnHelpers
62
- def active_scaffold_search_record_select(column, options)
63
- value = field_search_record_select_value(column)
64
- active_scaffold_record_select(column, options, value, column.options[:multiple])
65
- end
66
-
67
- def field_search_record_select_value(column)
68
- begin
69
- value = field_search_params[column.name]
70
- unless value.blank?
71
- if column.options[:multiple]
72
- column.association.klass.find value.collect!(&:to_i)
73
- else
74
- column.association.klass.find(value.to_i)
75
- end
76
- end
77
- rescue Exception => e
78
- logger.error Time.now.to_s + "Sorry, we are not that smart yet. Attempted to restore search values to search fields but instead got -- #{e.inspect} -- on the ActiveScaffold column = :#{column.name} in #{@controller.class}"
79
- raise e
80
- end
81
- end
82
- end
83
- end
84
- end
85
-
86
- ActionView::Base.class_eval { include ActiveScaffold::Bridges::RecordSelect::Helpers }