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.
@@ -1,187 +1,187 @@
1
- module RecordSelectHelper
2
- # Print this from your layout to include everything necessary for RecordSelect to work.
3
- # Well, not everything. You need Prototype too.
4
- def record_select_includes
5
- includes = ''
6
- includes << stylesheet_link_tag('record_select/record_select')
7
- includes << javascript_include_tag('record_select/record_select')
8
- includes
9
- end
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
- assert_controller_responds(options[:params][:controller])
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
- html = link_to_function(name, '', options[:html])
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
- return html
29
- end
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
- # Adds a RecordSelect-based form field. The field submits the record's id using a hidden input.
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
- url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
56
-
57
- html = text_field_tag(name, nil, :autocomplete => 'off', :id => options[:id], :class => options[:class], :onfocus => "this.focused=true", :onblur => "this.focused=false")
58
- html << javascript_tag("$(#{options[:id].to_json}).singleRecordSelect(#{url.to_json}, {id: #{id.to_json}, label: #{label.to_json}, onchange: #{options[:onchange] || ''.to_json}});")
59
-
60
- return html
61
- end
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
- # Assists with the creation of an observer for the :onchange option of the record_select_field method.
64
- # Currently only supports building an Ajax.Request based on the id of the selected record.
65
- #
66
- # options[:url] should be a hash with all the necessary options *except* :id. that parameter
67
- # will be provided based on the selected record.
68
- #
69
- # Question: if selecting users, what's more likely?
70
- # /users/5/categories
71
- # /categories?user_id=5
72
- def record_select_observer(options = {})
73
- fn = ""
74
- fn << "function(id, value) {"
75
- fn << "var url = #{url_for(options[:url].merge(:id => ":id:")).to_json}.replace(/:id:/, id);"
76
- fn << "jQuery.get(url);"
77
- fn << "}"
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
- # Adds a RecordSelect-based form field for multiple selections. The values submit using a list of hidden inputs.
81
- #
82
- # *Arguments*
83
- # +name+:: the input name that will be used to submit the selected records' ids. empty brackets will be appended to the name.
84
- # +current+:: pass a collection of existing associated records
85
- #
86
- # *Options*
87
- # +controller+:: The controller configured to provide the result set.
88
- # +params+:: A hash of extra URL parameters
89
- # +id+:: The id to use for the input. Defaults based on the input's name.
90
- def record_multi_select_field(name, current, options = {})
91
- options[:controller] ||= current.first.class.to_s.pluralize.underscore
92
- options[:params] ||= {}
93
- options[:id] ||= name.gsub(/[\[\]]/, '_')
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
- controller = assert_controller_responds(options[:controller])
110
+ controller = assert_controller_responds(options[:controller])
96
111
 
97
- current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
112
+ current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
98
113
 
99
- url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
114
+ url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
100
115
 
101
- html = text_field_tag("#{name}[]", nil, :autocomplete => 'off', :id => options[:id], :class => options[:class], :onfocus => "this.focused=true", :onblur => "this.focused=false")
102
- html << content_tag('ul', '', :class => 'record-select-list');
103
- html << javascript_tag("new RecordSelect.Multiple(#{options[:id].to_json}, #{url.to_json}, {current: #{current.to_json}});")
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
- return html
106
- end
120
+ return html
121
+ end
107
122
 
108
- # A helper to render RecordSelect partials
109
- def render_record_select(options = {}) #:nodoc:
110
- controller.send(:render_record_select, options) do |options|
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
- # The id of the RecordSelect widget for the given controller.
121
- def record_select_id(controller = nil) #:nodoc:
122
- controller ||= params[:controller]
123
- "record-select-#{controller.gsub('/', '_')}"
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
- def record_select_search_id(controller = nil) #:nodoc:
127
- "#{record_select_id(controller)}-search"
128
- end
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
- private
131
- # render the record using the renderer and add a link to select the record
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
- # uses renderer (defaults to record_select_config.label) to determine how the given record renders.
149
- def render_record_from_config(record, renderer = record_select_config.label)
150
- case renderer
151
- when Symbol, String
152
- # return full-html from the named partial
153
- render :partial => renderer.to_s, :locals => {:record => record}
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
- when Proc
156
- # return an html-cleaned descriptive string
157
- h renderer.call(record)
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
- # uses the result of render_record_from_config to snag an appropriate record label
162
- # to display in a field.
163
- #
164
- # if given a controller, searches for a partial in its views path
165
- def label_for_field(record, controller = self.controller)
166
- renderer = controller.record_select_config.label
167
- case renderer
168
- when Symbol, String
169
- # find the <label> element and grab its innerHTML
170
- description = render_record_from_config(record, File.join(controller.controller_path, renderer.to_s))
171
- description.match(/<label[^>]*>(.*)<\/label>/)[1]
172
-
173
- when Proc
174
- # just return the string
175
- render_record_from_config(record, renderer)
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
- def assert_controller_responds(controller_name)
180
- controller_name = "#{controller_name.camelize}Controller"
181
- controller = controller_name.constantize
182
- unless controller.uses_record_select?
183
- raise "#{controller_name} has not been configured to use RecordSelect."
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, RecordSelect::Helpers)
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|
@@ -1,3 +1,3 @@
1
1
  module RecordSelect
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: record_select
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - lion