recordselect 3.2.5 → 3.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/app/assets/images/record_select/cross.gif +0 -0
- data/app/assets/images/record_select/next.gif +0 -0
- data/app/assets/images/record_select/previous.gif +0 -0
- data/app/assets/javascripts/jquery/record_select.js +544 -0
- data/app/assets/javascripts/prototype/record_select.js +419 -0
- data/app/assets/javascripts/record_select.js.erb +5 -0
- data/app/assets/stylesheets/record_select.css.erb +133 -0
- data/app/views/record_select/_browse.html.erb +8 -0
- data/app/views/record_select/_list.html.erb +31 -0
- data/app/views/record_select/_search.html.erb +20 -0
- data/app/views/record_select/browse.js.erb +1 -0
- data/config/locales/en.yml +9 -0
- data/config/locales/es.yml +13 -0
- data/lib/record_select.rb +36 -0
- data/lib/record_select/actions.rb +68 -0
- data/lib/record_select/conditions.rb +100 -0
- data/lib/record_select/config.rb +103 -0
- data/lib/record_select/engine.rb +4 -0
- data/lib/record_select/extensions/active_record.rb +9 -0
- data/lib/record_select/extensions/localization.rb +13 -0
- data/lib/record_select/extensions/routing_mapper.rb +20 -0
- data/lib/record_select/form_builder.rb +25 -0
- data/lib/record_select/helpers/record_select_helper.rb +217 -0
- data/lib/record_select/version.rb +9 -0
- data/lib/recordselect.rb +14 -0
- metadata +39 -28
@@ -0,0 +1,25 @@
|
|
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
|
@@ -0,0 +1,217 @@
|
|
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
|
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
|
data/lib/recordselect.rb
ADDED
@@ -0,0 +1,14 @@
|
|
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)
|
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:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 3.2.
|
9
|
+
- 6
|
10
|
+
version: 3.2.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sergio Cambra
|
@@ -17,10 +17,11 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2013-
|
20
|
+
date: 2013-02-04 00:00:00 Z
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
23
|
-
|
23
|
+
type: :development
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
25
|
none: false
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
@@ -29,12 +30,12 @@ dependencies:
|
|
29
30
|
segments:
|
30
31
|
- 0
|
31
32
|
version: "0"
|
33
|
+
version_requirements: *id001
|
32
34
|
prerelease: false
|
33
|
-
type: :development
|
34
|
-
requirement: *id001
|
35
35
|
name: shoulda
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
|
37
|
+
type: :development
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
39
|
none: false
|
39
40
|
requirements:
|
40
41
|
- - ~>
|
@@ -45,26 +46,12 @@ dependencies:
|
|
45
46
|
- 0
|
46
47
|
- 0
|
47
48
|
version: 1.0.0
|
49
|
+
version_requirements: *id002
|
48
50
|
prerelease: false
|
49
|
-
type: :development
|
50
|
-
requirement: *id002
|
51
51
|
name: bundler
|
52
52
|
- !ruby/object:Gem::Dependency
|
53
|
-
|
54
|
-
|
55
|
-
requirements:
|
56
|
-
- - ">="
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
hash: 3
|
59
|
-
segments:
|
60
|
-
- 0
|
61
|
-
version: "0"
|
62
|
-
prerelease: false
|
63
|
-
type: :development
|
64
|
-
requirement: *id003
|
65
|
-
name: rcov
|
66
|
-
- !ruby/object:Gem::Dependency
|
67
|
-
version_requirements: &id004 !ruby/object:Gem::Requirement
|
53
|
+
type: :runtime
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
68
55
|
none: false
|
69
56
|
requirements:
|
70
57
|
- - ">="
|
@@ -75,9 +62,8 @@ dependencies:
|
|
75
62
|
- 1
|
76
63
|
- 3
|
77
64
|
version: 3.1.3
|
65
|
+
version_requirements: *id003
|
78
66
|
prerelease: false
|
79
|
-
type: :runtime
|
80
|
-
requirement: *id004
|
81
67
|
name: rails
|
82
68
|
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
|
83
69
|
email: activescaffold@googlegroups.com
|
@@ -88,6 +74,31 @@ extensions: []
|
|
88
74
|
extra_rdoc_files:
|
89
75
|
- README
|
90
76
|
files:
|
77
|
+
- app/assets/images/record_select/cross.gif
|
78
|
+
- app/assets/images/record_select/next.gif
|
79
|
+
- app/assets/images/record_select/previous.gif
|
80
|
+
- app/assets/javascripts/jquery/record_select.js
|
81
|
+
- app/assets/javascripts/prototype/record_select.js
|
82
|
+
- app/assets/javascripts/record_select.js.erb
|
83
|
+
- app/assets/stylesheets/record_select.css.erb
|
84
|
+
- app/views/record_select/_browse.html.erb
|
85
|
+
- app/views/record_select/_list.html.erb
|
86
|
+
- app/views/record_select/_search.html.erb
|
87
|
+
- app/views/record_select/browse.js.erb
|
88
|
+
- config/locales/en.yml
|
89
|
+
- config/locales/es.yml
|
90
|
+
- lib/record_select.rb
|
91
|
+
- lib/record_select/actions.rb
|
92
|
+
- lib/record_select/conditions.rb
|
93
|
+
- lib/record_select/config.rb
|
94
|
+
- lib/record_select/engine.rb
|
95
|
+
- lib/record_select/extensions/active_record.rb
|
96
|
+
- lib/record_select/extensions/localization.rb
|
97
|
+
- lib/record_select/extensions/routing_mapper.rb
|
98
|
+
- lib/record_select/form_builder.rb
|
99
|
+
- lib/record_select/helpers/record_select_helper.rb
|
100
|
+
- lib/record_select/version.rb
|
101
|
+
- lib/recordselect.rb
|
91
102
|
- MIT-LICENSE
|
92
103
|
- CHANGELOG
|
93
104
|
- README
|
@@ -121,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
132
|
requirements: []
|
122
133
|
|
123
134
|
rubyforge_project:
|
124
|
-
rubygems_version: 1.8.
|
135
|
+
rubygems_version: 1.8.24
|
125
136
|
signing_key:
|
126
137
|
specification_version: 3
|
127
138
|
summary: RecordSelect widget as a replacement for massive drop down lists
|