recordselect 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -191,6 +191,9 @@ RecordSelect.Abstract = Class.extend({
191
191
  this.url = url;
192
192
  this.options = options;
193
193
  this.container;
194
+ if (this.options.onchange && typeof(this.options.onchange) != 'function') {
195
+ this.options.onchange = eval(this.options.onchange);
196
+ }
194
197
 
195
198
  if (RecordSelect.document_loaded) {
196
199
  this.onload();
@@ -61,6 +61,9 @@ Object.extend(RecordSelect.Abstract.prototype, {
61
61
  this.url = url;
62
62
  this.options = options;
63
63
  this.container;
64
+ if (this.options.onchange && typeof(this.options.onchange) != 'function') {
65
+ this.options.onchange = eval(this.options.onchange);
66
+ }
64
67
 
65
68
  if (RecordSelect.document_loaded) this.onload();
66
69
  else Event.observe(window, 'load', this.onload.bind(this));
@@ -0,0 +1,14 @@
1
+ # Provides a simple pass-through localizer for RecordSelect. If you want
2
+ # to localize RS, you need to override this method and route it to your
3
+ # own system.
4
+ class Object
5
+ def rs_(string_to_localize, *args)
6
+ args.empty? ? string_to_localize : (sprintf string_to_localize, *args)
7
+ unless key.blank?
8
+ text = I18n.translate "#{key}", {:scope => [:record_select], :default => key.is_a?(String) ? key : key.to_s.titleize}.merge(options)
9
+ # text = nil if text.include?('translation missing:')
10
+ end
11
+ text ||= key
12
+ text
13
+ end
14
+ end
@@ -0,0 +1,209 @@
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
+ # +onchange+:: A JavaScript function that will be called whenever something new is selected. It should accept the new id as the first argument, and the new label as the second argument. For example, you could set onchange to be "function(id, label) {alert(id);}", or you could create a JavaScript function somewhere else and set onchange to be "my_function" (without the parantheses!).
34
+ def record_select_field(name, current, options = {})
35
+ options[:controller] ||= current.class.to_s.pluralize.underscore
36
+ options[:params] ||= {}
37
+ options[:id] ||= name.gsub(/[\[\]]/, '_')
38
+
39
+ id = options.delete(:id)
40
+ html = text_field_tag(name, nil, :autocomplete => 'off', :id => id, :class => options.delete(:class), :onfocus => "this.focused=true", :onblur => "this.focused=false")
41
+
42
+ controller = assert_controller_responds(options[:controller])
43
+
44
+ options[:id] = options[:label] = ''
45
+ if current and not current.new_record?
46
+ options[:id] = current.id
47
+ options[:label] = label_for_field(current, controller)
48
+ end
49
+
50
+ url = url_for({:action => :browse, :controller => options.delete(:controller), :escape => false}.merge(options.delete(:params)))
51
+ html << javascript_tag("new RecordSelect.Single(#{id.to_json}, #{url.to_json}, #{options.to_json});")
52
+
53
+ return html
54
+ end
55
+
56
+ # Adds a RecordSelect-based form field. The field is autocompleted.
57
+ #
58
+ # *Arguments*
59
+ # +name+:: the input name that will be used to submit the selected value.
60
+ # +current+:: the current object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.
61
+ #
62
+ # *Options*
63
+ # +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)
64
+ # +params+:: A hash of extra URL parameters
65
+ # +id+:: The id to use for the input. Defaults based on the input's name.
66
+ # +onchange+:: A JavaScript function that will be called whenever something new is selected. It should accept the new id as the first argument, and the new label as the second argument. For example, you could set onchange to be "function(id, label) {alert(id);}", or you could create a JavaScript function somewhere else and set onchange to be "my_function" (without the parantheses!).
67
+ def record_select_autocomplete(name, current, options = {})
68
+ options[:controller] ||= current.class.to_s.pluralize.underscore
69
+ options[:params] ||= {}
70
+ options[:id] ||= name.gsub(/[\[\]]/, '_')
71
+
72
+ html = text_field_tag(name, nil, :autocomplete => 'off', :id => options.delete(:id), :class => options.delete(:class), :onfocus => "this.focused=true", :onblur => "this.focused=false")
73
+
74
+ controller = assert_controller_responds(options[:controller])
75
+
76
+ options[:label] = ''
77
+ if current and not current.new_record?
78
+ options[:label] = label_for_field(current, controller)
79
+ end
80
+
81
+ url = url_for({:action => :browse, :controller => options.delete(:controller), :escape => false}.merge(options.delete(:params)))
82
+ html << javascript_tag("new RecordSelect.Autocomplete(#{options[:id].to_json}, #{url.to_json}, #{options.to_json});")
83
+
84
+ return html
85
+ end
86
+
87
+ # Assists with the creation of an observer for the :onchange option of the record_select_field method.
88
+ # Currently only supports building an Ajax.Request based on the id of the selected record.
89
+ #
90
+ # options[:url] should be a hash with all the necessary options *except* :id. that parameter
91
+ # will be provided based on the selected record.
92
+ #
93
+ # Question: if selecting users, what's more likely?
94
+ # /users/5/categories
95
+ # /categories?user_id=5
96
+ def record_select_observer(options = {})
97
+ fn = ""
98
+ fn << "function(id, value) {"
99
+ fn << "var url = #{url_for(options[:url].merge(:id => ":id:")).to_json}.replace(/:id:/, id);"
100
+ fn << "new Ajax.Request(url);"
101
+ fn << "}"
102
+ end
103
+
104
+ # Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.
105
+ #
106
+ # *Arguments*
107
+ # +name+:: the input name that will be used to submit the selected records' ids. empty brackets will be appended to the name.
108
+ # +current+:: pass a collection of existing associated records
109
+ #
110
+ # *Options*
111
+ # +controller+:: The controller configured to provide the result set.
112
+ # +params+:: A hash of extra URL parameters
113
+ # +id+:: The id to use for the input. Defaults based on the input's name.
114
+ def record_multi_select_field(name, current, options = {})
115
+ options[:controller] ||= current.first.class.to_s.pluralize.underscore
116
+ options[:params] ||= {}
117
+ options[:id] ||= name.gsub(/[\[\]]/, '_')
118
+
119
+ controller = assert_controller_responds(options[:controller])
120
+
121
+ # js identifier so we can talk to it.
122
+ widget = "rs_%s" % name.gsub(/[\[\]]/, '_').chomp('_')
123
+
124
+ current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
125
+
126
+ url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
127
+
128
+ html = text_field_tag("#{name}[]", nil, :autocomplete => 'off', :id => options[:id], :class => options[:class], :onfocus => "this.focused=true", :onblur => "this.focused=false")
129
+ html << content_tag('ul', '', :class => 'record-select-list');
130
+ html << javascript_tag("#{widget} = new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, {current: #{current.to_json}});")
131
+
132
+ return html
133
+ end
134
+
135
+ # A helper to render RecordSelect partials
136
+ def render_record_select(options = {}) #:nodoc:
137
+ controller.send(:render_record_select, options) do |options|
138
+ render options
139
+ end
140
+ end
141
+
142
+ # Provides view access to the RecordSelect configuration
143
+ def record_select_config #:nodoc:
144
+ controller.send :record_select_config
145
+ end
146
+
147
+ # The id of the RecordSelect widget for the given controller.
148
+ def record_select_id(controller = nil) #:nodoc:
149
+ controller ||= params[:controller]
150
+ "record-select-#{controller.gsub('/', '_')}"
151
+ end
152
+
153
+ def record_select_search_id(controller = nil) #:nodoc:
154
+ "#{record_select_id(controller)}-search"
155
+ end
156
+
157
+ private
158
+ # render the record using the renderer and add a link to select the record
159
+ def render_record_in_list(record, controller_path)
160
+ text = render_record_from_config(record)
161
+ if record_select_config.link?
162
+ url_options = {:controller => controller_path, :action => :select, :id => record.id, :escape => false}
163
+ link_to text, url_options, :method => :post, :remote => true, :class => ''
164
+ else
165
+ text
166
+ end
167
+ end
168
+
169
+
170
+ # uses renderer (defaults to record_select_config.label) to determine how the given record renders.
171
+ def render_record_from_config(record, renderer = record_select_config.label)
172
+ case renderer
173
+ when Symbol, String
174
+ # return full-html from the named partial
175
+ render :partial => renderer.to_s, :locals => {:record => record}
176
+
177
+ when Proc
178
+ # return an html-cleaned descriptive string
179
+ h renderer.call(record)
180
+ end
181
+ end
182
+
183
+ # uses the result of render_record_from_config to snag an appropriate record label
184
+ # to display in a field.
185
+ #
186
+ # if given a controller, searches for a partial in its views path
187
+ def label_for_field(record, controller = self.controller)
188
+ renderer = controller.record_select_config.label
189
+ case renderer
190
+ when Symbol, String
191
+ # find the <label> element and grab its innerHTML
192
+ description = render_record_from_config(record, File.join(controller.controller_path, renderer.to_s))
193
+ description.match(/<label[^>]*>(.*)<\/label>/)[1]
194
+
195
+ when Proc
196
+ # just return the string
197
+ render_record_from_config(record, renderer)
198
+ end
199
+ end
200
+
201
+ def assert_controller_responds(controller_name)
202
+ controller_name = "#{controller_name.camelize}Controller"
203
+ controller = controller_name.constantize
204
+ unless controller.uses_record_select?
205
+ raise "#{controller_name} has not been configured to use RecordSelect."
206
+ end
207
+ controller
208
+ end
209
+ end
@@ -2,7 +2,7 @@ module RecordSelect
2
2
  module Version
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- PATCH = 4
5
+ PATCH = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
@@ -0,0 +1,9 @@
1
+ module RecordSelect
2
+ module Version
3
+ MAJOR = 3
4
+ MINOR = 2
5
+ PATCH = 1
6
+
7
+ STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recordselect
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 4
10
- version: 3.0.4
9
+ - 5
10
+ version: 3.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sergio Cambra
@@ -17,11 +17,11 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-08-29 00:00:00 +02:00
21
- default_executable:
20
+ date: 2013-05-02 00:00:00 Z
22
21
  dependencies:
23
22
  - !ruby/object:Gem::Dependency
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ type: :development
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ">="
@@ -30,12 +30,12 @@ dependencies:
30
30
  segments:
31
31
  - 0
32
32
  version: "0"
33
- requirement: *id001
33
+ version_requirements: *id001
34
34
  prerelease: false
35
- type: :development
36
35
  name: shoulda
37
36
  - !ruby/object:Gem::Dependency
38
- version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ type: :development
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -46,12 +46,12 @@ dependencies:
46
46
  - 0
47
47
  - 0
48
48
  version: 1.0.0
49
- requirement: *id002
49
+ version_requirements: *id002
50
50
  prerelease: false
51
- type: :development
52
51
  name: bundler
53
52
  - !ruby/object:Gem::Dependency
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
53
+ type: :development
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
55
  none: false
56
56
  requirements:
57
57
  - - ">="
@@ -60,12 +60,12 @@ dependencies:
60
60
  segments:
61
61
  - 0
62
62
  version: "0"
63
- requirement: *id003
63
+ version_requirements: *id003
64
64
  prerelease: false
65
- type: :development
66
65
  name: rcov
67
66
  - !ruby/object:Gem::Dependency
68
- version_requirements: &id004 !ruby/object:Gem::Requirement
67
+ type: :runtime
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
69
  none: false
70
70
  requirements:
71
71
  - - ~>
@@ -76,9 +76,8 @@ dependencies:
76
76
  - 0
77
77
  - 0
78
78
  version: 3.0.0
79
- requirement: *id004
79
+ version_requirements: *id004
80
80
  prerelease: false
81
- type: :runtime
82
81
  name: rails
83
82
  description: RecordSelect is a Rails widget to help you pick one record out of many. I designed it as a more usable and performant alternative to generating a massive dropdown list
84
83
  email: activescaffold@googlegroups.com
@@ -99,8 +98,11 @@ files:
99
98
  - app/views/record_select/_search.html.erb
100
99
  - app/views/record_select/browse.js.rjs
101
100
  - app/views/record_select/_browse.html.erb
101
+ - app/views/record_select/_list.html.erb~
102
+ - app/assets/javascripts/jquery/record_select.js~
103
+ - app/assets/javascripts/prototype/record_select.js~
102
104
  - lib/record_select.rb
103
- - lib/record_select_assets.rb
105
+ - lib/recordselect.rb
104
106
  - lib/record_select/actions.rb
105
107
  - lib/record_select/conditions.rb
106
108
  - lib/record_select/config.rb
@@ -108,15 +110,17 @@ files:
108
110
  - lib/record_select/extensions/active_record.rb
109
111
  - lib/record_select/extensions/localization.rb
110
112
  - lib/record_select/extensions/routing_mapper.rb
113
+ - lib/record_select/extensions/localization.rb~
111
114
  - lib/record_select/helpers/record_select_helper.rb
115
+ - lib/record_select/helpers/record_select_helper.rb~
112
116
  - lib/record_select/version.rb
113
117
  - lib/record_select/engine.rb
114
- - lib/recordselect.rb
118
+ - lib/record_select/version.rb~
119
+ - lib/record_select_assets.rb
115
120
  - MIT-LICENSE
116
121
  - CHANGELOG
117
122
  - README
118
123
  - test/recordselect_test.rb
119
- has_rdoc: true
120
124
  homepage: http://github.com/scambra/recordselect
121
125
  licenses:
122
126
  - MIT
@@ -146,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
150
  requirements: []
147
151
 
148
152
  rubyforge_project:
149
- rubygems_version: 1.5.2
153
+ rubygems_version: 1.8.23
150
154
  signing_key:
151
155
  specification_version: 3
152
156
  summary: RecordSelect widget as a replacement for massive drop down lists