record_select 0.0.2 → 0.0.3

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/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in record_select.gemspec
3
+ # Specify your gem's dependencies in recordselect.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,29 +0,0 @@
1
- # RecordSelect
2
-
3
- TODO: Write a gem description
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'record_select'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install record_select
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Contributing
24
-
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
@@ -1,187 +1,187 @@
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
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
11
10
 
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)}"
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)}"
23
22
 
24
- assert_controller_responds(options[:params][:controller])
23
+ assert_controller_responds(options[:params][:controller])
25
24
 
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] || ''}})")
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] || ''}})")
28
27
 
29
- return html
30
- end
28
+ return html
29
+ end
31
30
 
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
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)
77
53
  end
78
54
 
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
55
+ url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
94
56
 
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(/[\[\]]/, '_')
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}});")
109
59
 
110
- controller = assert_controller_responds(options[:controller])
60
+ return html
61
+ end
111
62
 
112
- current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
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
113
79
 
114
- url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
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(/[\[\]]/, '_')
115
94
 
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}});")
95
+ controller = assert_controller_responds(options[:controller])
119
96
 
120
- return html
121
- end
97
+ current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
98
+
99
+ url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
100
+
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}});")
104
+
105
+ return html
106
+ end
122
107
 
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
108
+ # A helper to render RecordSelect partials
109
+ def render_record_select(options = {}) #:nodoc:
110
+ controller.send(:render_record_select, options) do |options|
127
111
  render options
128
112
  end
113
+ end
129
114
 
130
- # Provides view access to the RecordSelect configuration
131
- def record_select_config #:nodoc:
132
- controller.send :record_select_config
133
- end
115
+ # Provides view access to the RecordSelect configuration
116
+ def record_select_config #:nodoc:
117
+ controller.send :record_select_config
118
+ end
134
119
 
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
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
140
125
 
141
- def record_select_search_id(controller = nil) #:nodoc:
142
- "#{record_select_id(controller)}-search"
126
+ def record_select_search_id(controller = nil) #:nodoc:
127
+ "#{record_select_id(controller)}-search"
128
+ end
129
+
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
143
144
  end
145
+ end
144
146
 
145
- private
146
147
 
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}
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}
153
154
 
154
- when Proc
155
- # return an html-cleaned descriptive string
156
- h renderer.call(record)
157
- end
155
+ when Proc
156
+ # return an html-cleaned descriptive string
157
+ h renderer.call(record)
158
158
  end
159
+ end
159
160
 
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
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)
176
176
  end
177
+ end
177
178
 
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
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."
185
184
  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, RecordSelectHelper)
5
+ ActionView::Base.send(:include, RecordSelect::Helpers)
6
6
  ActionView::Helpers::FormBuilder.send(:include, RecordSelect::FormBuilder)
7
7
 
8
8
  ['stylesheets', 'images', 'javascripts'].each do |asset_type|
@@ -0,0 +1,33 @@
1
+ module RecordSelect
2
+ def self.included(base)
3
+ base.send :extend, ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ # Enables and configures RecordSelect on your controller.
8
+ #
9
+ # *Options*
10
+ # +model+:: defaults based on the name of the controller
11
+ # +per_page+:: records to show per page when browsing
12
+ # +notify+:: a method name to invoke when a record has been selected.
13
+ # +order_by+:: a SQL string to order the search results
14
+ # +search_on+:: an array of searchable fields
15
+ # +full_text_search+:: a boolean for whether to use a %?% search pattern or not. default is false.
16
+ # +label+:: a proc that accepts a record as argument and returns an option label. default is to call record.to_label instead.
17
+ # +include+:: as for ActiveRecord::Base#find. can help with search conditions or just help optimize rendering the results.
18
+ #
19
+ # You may also pass a block, which will be used as options[:notify].
20
+ def record_select(options = {})
21
+ options[:model] ||= self.to_s.split('::').last.sub(/Controller$/, '').pluralize.singularize.underscore
22
+ @record_select_config = RecordSelect::Config.new(options.delete(:model), options)
23
+ self.send :include, RecordSelect::Actions
24
+ self.send :include, RecordSelect::Conditions
25
+ end
26
+
27
+ attr_reader :record_select_config
28
+
29
+ def uses_record_select?
30
+ !record_select_config.nil?
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module RecordselectCustom
2
+ VERSION = "0.0.3"
3
+ end
@@ -5,11 +5,11 @@ require 'record_select/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "record_select"
8
- gem.version = RecordSelect::VERSION
8
+ gem.version = RecordselectCustom::VERSION
9
9
  gem.authors = ["lion"]
10
10
  gem.email = ["lion3139@gmail.com"]
11
- gem.description = %q{just test}
12
- gem.summary = %q{just test}
11
+ gem.description = %q{just for test}
12
+ gem.summary = %q{just for test}
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - lion
@@ -19,7 +19,7 @@ date: 2014-12-17 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: just test
22
+ description: just for test
23
23
  email:
24
24
  - lion3139@gmail.com
25
25
  executables: []
@@ -59,13 +59,14 @@ files:
59
59
  - lib/record_select/config.rb
60
60
  - lib/record_select/form_builder.rb
61
61
  - lib/record_select/helpers.rb
62
- - lib/record_select/version.rb
62
+ - lib/recordselect_custom.rb
63
+ - lib/recordselect_custom/version.rb
63
64
  - lib/views/_browse.html
64
65
  - lib/views/_browse.rhtml
65
66
  - lib/views/_list.rhtml
66
67
  - lib/views/_search.rhtml
67
68
  - lib/views/browse.rjs
68
- - record_select.gemspec
69
+ - recordselect_custom.gemspec
69
70
  - test/recordselect_test.rb
70
71
  - uninstall.rb
71
72
  has_rdoc: true
@@ -101,6 +102,6 @@ rubyforge_project:
101
102
  rubygems_version: 1.3.9.5
102
103
  signing_key:
103
104
  specification_version: 3
104
- summary: just test
105
+ summary: just for test
105
106
  test_files:
106
107
  - test/recordselect_test.rb
@@ -1,3 +0,0 @@
1
- module RecordSelect
2
- VERSION = "0.0.2"
3
- end