record_select 0.0.1 → 0.0.2
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.
- data/app/helpers/record_select_helper.rb +157 -157
- data/init.rb +1 -1
- data/lib/record_select/version.rb +1 -1
- metadata +3 -3
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
module
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
# Adds a link on the page that toggles a RecordSelect widget from the given controller.
|
|
12
|
-
#
|
|
13
|
-
# *Options*
|
|
14
|
-
# +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.
|
|
15
|
-
# +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.
|
|
16
|
-
def link_to_record_select(name, controller, options = {})
|
|
17
|
-
options[:params] ||= {}
|
|
18
|
-
options[:params].merge!(:controller => controller, :action => :browse)
|
|
19
|
-
options[:onselect] = "function(id, label) {#{options[:onselect]}}" if options[:onselect]
|
|
20
|
-
options[:html] ||= {}
|
|
21
|
-
options[:html][:id] ||= "rs_#{rand(9999)}"
|
|
1
|
+
module RecordSelect
|
|
2
|
+
module Helpers
|
|
3
|
+
# Print this from your layout to include everything necessary for RecordSelect to work.
|
|
4
|
+
# Well, not everything. You need Prototype too.
|
|
5
|
+
def record_select_includes
|
|
6
|
+
includes = ''
|
|
7
|
+
includes << stylesheet_link_tag('record_select/record_select')
|
|
8
|
+
includes << javascript_include_tag('record_select/record_select')
|
|
9
|
+
includes
|
|
10
|
+
end
|
|
22
11
|
|
|
23
|
-
|
|
12
|
+
# Adds a link on the page that toggles a RecordSelect widget from the given controller.
|
|
13
|
+
#
|
|
14
|
+
# *Options*
|
|
15
|
+
# +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.
|
|
16
|
+
# +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.
|
|
17
|
+
def link_to_record_select(name, controller, options = {})
|
|
18
|
+
options[:params] ||= {}
|
|
19
|
+
options[:params].merge!(:controller => controller, :action => :browse)
|
|
20
|
+
options[:onselect] = "function(id, label) {#{options[:onselect]}}" if options[:onselect]
|
|
21
|
+
options[:html] ||= {}
|
|
22
|
+
options[:html][:id] ||= "rs_#{rand(9999)}"
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
html << javascript_tag("new RecordSelect.Dialog(#{options[:html][:id].to_json}, #{url_for(options[:params].merge(:escape => false)).to_json}, {onselect: #{options[:onselect] || ''}})")
|
|
24
|
+
assert_controller_responds(options[:params][:controller])
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
html = link_to_function(name, '', options[:html])
|
|
27
|
+
html << javascript_tag("new RecordSelect.Dialog(#{options[:html][:id].to_json}, #{url_for(options[:params].merge(:escape => false)).to_json}, {onselect: #{options[:onselect] || ''}})")
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
#
|
|
33
|
-
# *Arguments*
|
|
34
|
-
# +name+:: the input name that will be used to submit the selected record's id.
|
|
35
|
-
# +current+:: the currently selected object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.
|
|
36
|
-
#
|
|
37
|
-
# *Options*
|
|
38
|
-
# +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)
|
|
39
|
-
# +params+:: A hash of extra URL parameters
|
|
40
|
-
# +id+:: The id to use for the input. Defaults based on the input's name.
|
|
41
|
-
# +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!).
|
|
42
|
-
def record_select_field(name, current, options = {})
|
|
43
|
-
options[:controller] ||= current.class.to_s.pluralize.underscore
|
|
44
|
-
options[:params] ||= {}
|
|
45
|
-
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
46
|
-
|
|
47
|
-
controller = assert_controller_responds(options[:controller])
|
|
48
|
-
|
|
49
|
-
id = label = ''
|
|
50
|
-
if current and not current.new_record?
|
|
51
|
-
id = current.id
|
|
52
|
-
label = label_for_field(current, controller)
|
|
29
|
+
return html
|
|
53
30
|
end
|
|
54
31
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
32
|
+
# Adds a RecordSelect-based form field. The field submits the record's id using a hidden input.
|
|
33
|
+
#
|
|
34
|
+
# *Arguments*
|
|
35
|
+
# +name+:: the input name that will be used to submit the selected record's id.
|
|
36
|
+
# +current+:: the currently selected object. provide a new record if there're none currently selected and you have not passed the optional :controller argument.
|
|
37
|
+
#
|
|
38
|
+
# *Options*
|
|
39
|
+
# +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)
|
|
40
|
+
# +params+:: A hash of extra URL parameters
|
|
41
|
+
# +id+:: The id to use for the input. Defaults based on the input's name.
|
|
42
|
+
# +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!).
|
|
43
|
+
def record_select_field(name, current, options = {})
|
|
44
|
+
options[:controller] ||= current.class.to_s.pluralize.underscore
|
|
45
|
+
options[:params] ||= {}
|
|
46
|
+
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
47
|
+
options[:js_params] ||= "null"
|
|
48
|
+
options[:ass_value] = true if options[:ass_value].nil?
|
|
49
|
+
|
|
50
|
+
controller = assert_controller_responds(options[:controller])
|
|
51
|
+
|
|
52
|
+
id = label = ''
|
|
53
|
+
if options[:ass_value]
|
|
54
|
+
if current and not current.new_record?
|
|
55
|
+
id = current.id
|
|
56
|
+
label = label_for_field(current, controller)
|
|
57
|
+
end
|
|
58
|
+
else
|
|
59
|
+
id = current.object_id
|
|
60
|
+
label = current
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
url = url_for({:action => :browse, :controller => options[:controller], :escape => false, :format => :js}.merge(options[:params]))
|
|
64
|
+
|
|
65
|
+
html = text_field_tag(name, nil, :autocomplete => 'off', :id => options[:id], :class => options[:class],
|
|
66
|
+
:onfocus => "this.focused=true", :onblur => "this.focused=false", :placeholder => options[:placeholder])
|
|
67
|
+
html << javascript_tag("jQuery(#{("#"+options[:id]).to_json}).recordSelectSingle(#{url.to_json}, {
|
|
68
|
+
id: #{id.to_json},
|
|
69
|
+
label: #{label.to_json},
|
|
70
|
+
ass_value: #{options[:ass_value]},
|
|
71
|
+
openEvents: #{options[:open_events].try(:to_json) || 'null'},
|
|
72
|
+
onchange: #{options[:onchange] || ''.to_json},
|
|
73
|
+
js_params: #{options[:js_params]}
|
|
74
|
+
});")
|
|
75
|
+
|
|
76
|
+
return html
|
|
77
|
+
end
|
|
62
78
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
end
|
|
79
|
+
# Assists with the creation of an observer for the :onchange option of the record_select_field method.
|
|
80
|
+
# Currently only supports building an Ajax.Request based on the id of the selected record.
|
|
81
|
+
#
|
|
82
|
+
# options[:url] should be a hash with all the necessary options *except* :id. that parameter
|
|
83
|
+
# will be provided based on the selected record.
|
|
84
|
+
#
|
|
85
|
+
# Question: if selecting users, what's more likely?
|
|
86
|
+
# /users/5/categories
|
|
87
|
+
# /categories?user_id=5
|
|
88
|
+
def record_select_observer(options = {})
|
|
89
|
+
fn = "function(id, value) {"
|
|
90
|
+
fn << "var url = #{url_for(options[:url].merge(:id => ":id:")).to_json}.replace(/:id:/, id);"
|
|
91
|
+
fn << "jQuery.get(url);"
|
|
92
|
+
fn << "}"
|
|
93
|
+
end
|
|
79
94
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
95
|
+
# Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.
|
|
96
|
+
#
|
|
97
|
+
# *Arguments*
|
|
98
|
+
# +name+:: the input name that will be used to submit the selected records' ids. empty brackets will be appended to the name.
|
|
99
|
+
# +current+:: pass a collection of existing associated records
|
|
100
|
+
#
|
|
101
|
+
# *Options*
|
|
102
|
+
# +controller+:: The controller configured to provide the result set.
|
|
103
|
+
# +params+:: A hash of extra URL parameters
|
|
104
|
+
# +id+:: The id to use for the input. Defaults based on the input's name.
|
|
105
|
+
def record_multi_select_field(name, current, options = {})
|
|
106
|
+
options[:controller] ||= current.first.class.to_s.pluralize.underscore
|
|
107
|
+
options[:params] ||= {}
|
|
108
|
+
options[:id] ||= name.gsub(/[\[\]]/, '_')
|
|
94
109
|
|
|
95
|
-
|
|
110
|
+
controller = assert_controller_responds(options[:controller])
|
|
96
111
|
|
|
97
|
-
|
|
112
|
+
current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
|
|
98
113
|
|
|
99
|
-
|
|
114
|
+
url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
|
|
100
115
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
116
|
+
html = text_field_tag("#{name}[]", nil, :autocomplete => 'off', :id => options[:id], :class => options[:class], :onfocus => "this.focused=true", :onblur => "this.focused=false")
|
|
117
|
+
html << content_tag('ul', '', :class => 'record-select-list');
|
|
118
|
+
html << javascript_tag("new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, {current: #{current.to_json}});")
|
|
104
119
|
|
|
105
|
-
|
|
106
|
-
|
|
120
|
+
return html
|
|
121
|
+
end
|
|
107
122
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
123
|
+
# A helper to render RecordSelect partials
|
|
124
|
+
def render_record_select(file, options = {}) #:nodoc:
|
|
125
|
+
options[:file] = controller.send(:record_select_path_of, file)
|
|
126
|
+
options[:use_full_path] = false
|
|
111
127
|
render options
|
|
112
128
|
end
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
# Provides view access to the RecordSelect configuration
|
|
116
|
-
def record_select_config #:nodoc:
|
|
117
|
-
controller.send :record_select_config
|
|
118
|
-
end
|
|
119
129
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
end
|
|
130
|
+
# Provides view access to the RecordSelect configuration
|
|
131
|
+
def record_select_config #:nodoc:
|
|
132
|
+
controller.send :record_select_config
|
|
133
|
+
end
|
|
125
134
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
135
|
+
# The id of the RecordSelect widget for the given controller.
|
|
136
|
+
def record_select_id(controller = nil) #:nodoc:
|
|
137
|
+
controller ||= params[:controller]
|
|
138
|
+
"record-select-#{controller.gsub('/', '_')}"
|
|
139
|
+
end
|
|
129
140
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
def render_record_in_list(record, controller_path)
|
|
133
|
-
text = render_record_from_config(record)
|
|
134
|
-
if record_select_config.link?
|
|
135
|
-
url_params = {:controller => controller_path, :action => :select, :id => record.id, :escape => false}
|
|
136
|
-
link_to_remote text, {
|
|
137
|
-
:url => url_params,
|
|
138
|
-
:method => :post,
|
|
139
|
-
:before => 'jQuery(this).toggleClass("selected")',
|
|
140
|
-
:condition => "jQuery(this).recordSelect_notify()"
|
|
141
|
-
}, :href => url_params
|
|
142
|
-
else
|
|
143
|
-
text
|
|
141
|
+
def record_select_search_id(controller = nil) #:nodoc:
|
|
142
|
+
"#{record_select_id(controller)}-search"
|
|
144
143
|
end
|
|
145
|
-
end
|
|
146
144
|
|
|
145
|
+
private
|
|
147
146
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
147
|
+
# uses renderer (defaults to record_select_config.label) to determine how the given record renders.
|
|
148
|
+
def render_record_from_config(record, renderer = record_select_config.label)
|
|
149
|
+
case renderer
|
|
150
|
+
when Symbol, String
|
|
151
|
+
# return full-html from the named partial
|
|
152
|
+
render :partial => renderer.to_s, :locals => {:record => record}
|
|
154
153
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
154
|
+
when Proc
|
|
155
|
+
# return an html-cleaned descriptive string
|
|
156
|
+
h renderer.call(record)
|
|
157
|
+
end
|
|
158
158
|
end
|
|
159
|
-
end
|
|
160
159
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
160
|
+
# uses the result of render_record_from_config to snag an appropriate record label
|
|
161
|
+
# to display in a field.
|
|
162
|
+
#
|
|
163
|
+
# if given a controller, searches for a partial in its views path
|
|
164
|
+
def label_for_field(record, controller = self.controller)
|
|
165
|
+
renderer = controller.record_select_config.label
|
|
166
|
+
case renderer
|
|
167
|
+
when Symbol, String
|
|
168
|
+
# find the <label> element and grab its innerHTML
|
|
169
|
+
description = render_record_from_config(record, File.join(controller.controller_path, renderer.to_s))
|
|
170
|
+
description.match(/<label[^>]*>(.*)<\/label>/)[1]
|
|
171
|
+
|
|
172
|
+
when Proc
|
|
173
|
+
# just return the string
|
|
174
|
+
render_record_from_config(record, renderer)
|
|
175
|
+
end
|
|
176
176
|
end
|
|
177
|
-
end
|
|
178
177
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
178
|
+
def assert_controller_responds(controller_name)
|
|
179
|
+
controller_name = "#{controller_name.camelize}Controller"
|
|
180
|
+
controller = controller_name.constantize
|
|
181
|
+
unless controller.uses_record_select?
|
|
182
|
+
raise "#{controller_name} has not been configured to use RecordSelect."
|
|
183
|
+
end
|
|
184
|
+
controller
|
|
184
185
|
end
|
|
185
|
-
controller
|
|
186
186
|
end
|
|
187
187
|
end
|
data/init.rb
CHANGED
|
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/lib/localization'
|
|
|
2
2
|
require File.dirname(__FILE__) + '/lib/extensions/active_record'
|
|
3
3
|
|
|
4
4
|
ActionController::Base.send(:include, RecordSelect)
|
|
5
|
-
ActionView::Base.send(:include,
|
|
5
|
+
ActionView::Base.send(:include, RecordSelectHelper)
|
|
6
6
|
ActionView::Helpers::FormBuilder.send(:include, RecordSelect::FormBuilder)
|
|
7
7
|
|
|
8
8
|
['stylesheets', 'images', 'javascripts'].each do |asset_type|
|
metadata
CHANGED