recordselect 3.0.5 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +0,0 @@
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
@@ -1,209 +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
- # +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
@@ -1,9 +0,0 @@
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
@@ -1,28 +0,0 @@
1
- class RecordSelectAssets
2
-
3
- def self.copy_to_public(options = {})
4
- if defined? ActiveScaffold
5
- RecordSelect::Config.js_framework = ActiveScaffold.js_framework
6
- else
7
- RecordSelect::Config.js_framework = :jquery
8
- end
9
- unless defined?(RECORD_SELECT_INSTALL_ASSETS) && RECORD_SELECT_INSTALL_ASSETS == false
10
- ['stylesheets', 'images', 'javascripts'].each do |asset_type|
11
- if asset_type == 'javascripts'
12
- local_dir = File.join(File.dirname(__FILE__), '..', 'assets', asset_type, RecordSelect::Config.js_framework.to_s)
13
- else
14
- local_dir = File.join(File.dirname(__FILE__), '..', 'assets', asset_type)
15
- end
16
- public_dir = File.join(Rails.root, 'public', asset_type, 'record_select')
17
- FileUtils.mkdir public_dir unless File.exists? public_dir
18
- Dir.entries(local_dir).each do |file|
19
- next if file =~ /^\./
20
- FileUtils.cp File.join(local_dir, file), public_dir
21
- end
22
- end
23
- end
24
- end
25
-
26
- protected
27
-
28
- end