recordselect 3.2.4 → 3.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +0,0 @@
1
- module RecordSelect
2
- module FormBuilder
3
- def record_select(association, options = {})
4
- reflection = @object.class.reflect_on_association(association)
5
- form_name = form_name_for_association(reflection)
6
- current = @object.send(association)
7
- options[:id] ||= "#{@object_name.gsub(/[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")}_#{association}"
8
-
9
- if [:has_one, :belongs_to].include? reflection.macro
10
- @template.record_select_field(form_name, current || reflection.klass.new, options)
11
- else
12
- options[:controller] ||= reflection.klass.to_s.pluralize.underscore
13
- @template.record_multi_select_field(form_name, current, options)
14
- end
15
- end
16
-
17
- private
18
-
19
- def form_name_for_association(reflection)
20
- key_name = (reflection.options[:foreign_key] || reflection.association_foreign_key)
21
- key_name += "s" unless [:has_one, :belongs_to].include? reflection.macro
22
- form_name = "#{@object_name}[#{key_name}]"
23
- end
24
- end
25
- end
@@ -1,217 +0,0 @@
1
- module RecordSelectHelper
2
- # Adds a link on the page that toggles a RecordSelect widget from the given controller.
3
- #
4
- # *Options*
5
- # +onselect+:: JavaScript code to handle selections client-side. This code has access to two variables: id, label. If the code returns false, the dialog will *not* close automatically.
6
- # +params+:: Extra URL parameters. If any parameter is a column name, the parameter will be used as a search term to filter the result set.
7
- def link_to_record_select(name, controller, options = {})
8
- options[:params] ||= {}
9
- options[:params].merge!(:controller => controller, :action => :browse)
10
- options[:onselect] = "function(id, label) {#{options[:onselect]}}" if options[:onselect]
11
- options[:html] ||= {}
12
- options[:html][:id] ||= "rs_#{rand(9999)}"
13
-
14
- assert_controller_responds(options[:params][:controller])
15
-
16
- html = link_to_function(name, '', options[:html])
17
- html << javascript_tag("new RecordSelect.Dialog(#{options[:html][:id].to_json}, #{url_for(options[:params].merge(:escape => false)).to_json}, {onselect: #{options[:onselect] || ''}})")
18
-
19
- return html
20
- end
21
-
22
- # Adds a RecordSelect-based form field. The field submits the record's id using a hidden input.
23
- #
24
- # *Arguments*
25
- # +name+:: the input name that will be used to submit the selected record's id.
26
- # +current+:: the currently selected object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.
27
- #
28
- # *Options*
29
- # +controller+:: The controller configured to provide the result set. Optional if you have standard resource controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the class of +current+ (the second argument)
30
- # +params+:: A hash of extra URL parameters
31
- # +id+:: The id to use for the input. Defaults based on the input's name.
32
- # +field_name+:: The name to use for the text input. Defaults to '', so field is not submitted.
33
- def record_select_field(name, current, options = {})
34
- options[:controller] ||= current.class.to_s.pluralize.underscore
35
- options[:params] ||= {}
36
- options[:id] ||= name.gsub(/[\[\]]/, '_')
37
- options[:class] ||= ''
38
- options[:class] << ' recordselect'
39
-
40
- ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]
41
-
42
- controller = assert_controller_responds(options.delete(:controller))
43
- params = options.delete(:params)
44
- record_select_options = {}
45
- record_select_options[:field_name] = options.delete(:field_name) if options[:field_name]
46
- if current and not current.new_record?
47
- record_select_options[:id] = current.id
48
- record_select_options[:label] = label_for_field(current, controller)
49
- end
50
-
51
- html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
52
- url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
53
- html << javascript_tag("new RecordSelect.Single(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")
54
-
55
- return html
56
- end
57
-
58
- # Adds a RecordSelect-based form field. The field is autocompleted.
59
- #
60
- # *Arguments*
61
- # +name+:: the input name that will be used to submit the selected value.
62
- # +current+:: the current object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.
63
- #
64
- # *Options*
65
- # +controller+:: The controller configured to provide the result set. Optional if you have standard resource controllers (e.g. UsersController for the User model), in which case the controller will be inferred from the class of +current+ (the second argument)
66
- # +params+:: A hash of extra URL parameters
67
- # +id+:: The id to use for the input. Defaults based on the input's name.
68
- def record_select_autocomplete(name, current, options = {})
69
- options[:controller] ||= current.class.to_s.pluralize.underscore
70
- options[:params] ||= {}
71
- options[:id] ||= name.gsub(/[\[\]]/, '_')
72
- options[:class] ||= ''
73
- options[:class] << ' recordselect'
74
-
75
- ActiveSupport::Deprecation.warn 'onchange option is deprecated. Bind recordselect:change event instead.' if options[:onchange]
76
-
77
- controller = assert_controller_responds(options.delete(:controller))
78
- params = options.delete(:params)
79
- record_select_options = {}
80
- if current and not current.new_record?
81
- record_select_options[:label] = label_for_field(current, controller)
82
- end
83
-
84
- html = text_field_tag(name, nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
85
- url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
86
- html << javascript_tag("new RecordSelect.Autocomplete(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")
87
-
88
- return html
89
- end
90
-
91
- # Assists with the creation of an observer for the :onchange option of the record_select_field method.
92
- # Currently only supports building an Ajax.Request based on the id of the selected record.
93
- #
94
- # options[:url] should be a hash with all the necessary options *except* :id. that parameter
95
- # will be provided based on the selected record.
96
- #
97
- # Question: if selecting users, what's more likely?
98
- # /users/5/categories
99
- # /categories?user_id=5
100
- def record_select_observer(options = {})
101
- fn = ""
102
- fn << "function(id, value) {"
103
- fn << "var url = #{url_for(options[:url].merge(:id => ":id:")).to_json}.replace(/:id:/, id);"
104
- fn << "new Ajax.Request(url);"
105
- fn << "}"
106
- end
107
-
108
- # Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.
109
- #
110
- # *Arguments*
111
- # +name+:: the input name that will be used to submit the selected records' ids. empty brackets will be appended to the name.
112
- # +current+:: pass a collection of existing associated records
113
- #
114
- # *Options*
115
- # +controller+:: The controller configured to provide the result set.
116
- # +params+:: A hash of extra URL parameters
117
- # +id+:: The id to use for the input. Defaults based on the input's name.
118
- def record_multi_select_field(name, current, options = {})
119
- options[:controller] ||= current.first.class.to_s.pluralize.underscore
120
- options[:params] ||= {}
121
- options[:id] ||= name.gsub(/[\[\]]/, '_')
122
- options[:class] ||= ''
123
- options[:class] << ' recordselect'
124
- options.delete(:name)
125
-
126
- controller = assert_controller_responds(options.delete(:controller))
127
- params = options.delete(:params)
128
- record_select_options = {}
129
- record_select_options[:current] = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
130
-
131
- html = text_field_tag("#{name}[]", nil, options.merge(:autocomplete => 'off', :onfocus => "this.focused=true", :onblur => "this.focused=false"))
132
- html << hidden_field_tag("#{name}[]", '', :id => nil)
133
- html << content_tag('ul', '', :class => 'record-select-list');
134
-
135
- # js identifier so we can talk to it.
136
- widget = "rs_%s" % name.gsub(/[\[\]]/, '_').chomp('_')
137
- url = url_for({:action => :browse, :controller => controller.controller_path, :escape => false}.merge(params))
138
- html << javascript_tag("#{widget} = new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, #{record_select_options.to_json});")
139
-
140
- return html
141
- end
142
-
143
- # A helper to render RecordSelect partials
144
- def render_record_select(options = {}) #:nodoc:
145
- controller.send(:render_record_select, options) do |options|
146
- render options
147
- end
148
- end
149
-
150
- # Provides view access to the RecordSelect configuration
151
- def record_select_config #:nodoc:
152
- controller.send :record_select_config
153
- end
154
-
155
- # The id of the RecordSelect widget for the given controller.
156
- def record_select_id(controller = nil) #:nodoc:
157
- controller ||= params[:controller]
158
- "record-select-#{controller.gsub('/', '_')}"
159
- end
160
-
161
- def record_select_search_id(controller = nil) #:nodoc:
162
- "#{record_select_id(controller)}-search"
163
- end
164
-
165
- private
166
- # render the record using the renderer and add a link to select the record
167
- def render_record_in_list(record, controller_path)
168
- text = render_record_from_config(record)
169
- if record_select_config.link?
170
- url_options = {:controller => controller_path, :action => :select, :id => record.id, :escape => false}
171
- link_to text, url_options, :method => :post, :remote => true, :class => ''
172
- else
173
- text
174
- end
175
- end
176
-
177
-
178
- # uses renderer (defaults to record_select_config.label) to determine how the given record renders.
179
- def render_record_from_config(record, renderer = record_select_config.label)
180
- case renderer
181
- when Symbol, String
182
- # return full-html from the named partial
183
- render :partial => renderer.to_s, :locals => {:record => record}
184
-
185
- when Proc
186
- # return an html-cleaned descriptive string
187
- h renderer.call(record)
188
- end
189
- end
190
-
191
- # uses the result of render_record_from_config to snag an appropriate record label
192
- # to display in a field.
193
- #
194
- # if given a controller, searches for a partial in its views path
195
- def label_for_field(record, controller = self.controller)
196
- renderer = controller.record_select_config.label
197
- case renderer
198
- when Symbol, String
199
- # find the <label> element and grab its innerHTML
200
- description = render_record_from_config(record, File.join(controller.controller_path, renderer.to_s))
201
- description.match(/<label[^>]*>(.*)<\/label>/)[1]
202
-
203
- when Proc
204
- # just return the string
205
- render_record_from_config(record, renderer)
206
- end
207
- end
208
-
209
- def assert_controller_responds(controller_name)
210
- controller_name = "#{controller_name.camelize}Controller"
211
- controller = controller_name.constantize
212
- unless controller.uses_record_select?
213
- raise "#{controller_name} has not been configured to use RecordSelect."
214
- end
215
- controller
216
- end
217
- end
@@ -1,9 +0,0 @@
1
- module RecordSelect
2
- module Version
3
- MAJOR = 3
4
- MINOR = 2
5
- PATCH = 4
6
-
7
- STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
- end
9
- end
data/lib/recordselect.rb DELETED
@@ -1,14 +0,0 @@
1
- require 'record_select'
2
- require 'record_select/extensions/localization'
3
- require 'record_select/extensions/active_record'
4
- require 'record_select/extensions/routing_mapper'
5
- require 'record_select/actions'
6
- require 'record_select/conditions'
7
- require 'record_select/config'
8
- require 'record_select/form_builder'
9
- require 'record_select/helpers/record_select_helper'
10
- require 'record_select/engine' unless defined? RECORD_SELECT_PLUGIN
11
-
12
- ActionController::Base.send(:include, RecordSelect)
13
- ActionView::Base.send(:include, RecordSelectHelper)
14
- ActionView::Helpers::FormBuilder.send(:include, RecordSelect::FormBuilder)