recordselect-custom 0.0.1

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.
@@ -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,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
11
+
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)}"
23
+
24
+ assert_controller_responds(options[:params][:controller])
25
+
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] || ''}})")
28
+
29
+ return html
30
+ end
31
+
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
78
+
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
94
+
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(/[\[\]]/, '_')
109
+
110
+ controller = assert_controller_responds(options[:controller])
111
+
112
+ current = current.inject([]) { |memo, record| memo.push({:id => record.id, :label => label_for_field(record, controller)}) }
113
+
114
+ url = url_for({:action => :browse, :controller => options[:controller], :escape => false}.merge(options[:params]))
115
+
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}});")
119
+
120
+ return html
121
+ end
122
+
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
127
+ render options
128
+ end
129
+
130
+ # Provides view access to the RecordSelect configuration
131
+ def record_select_config #:nodoc:
132
+ controller.send :record_select_config
133
+ end
134
+
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
140
+
141
+ def record_select_search_id(controller = nil) #:nodoc:
142
+ "#{record_select_id(controller)}-search"
143
+ end
144
+
145
+ private
146
+
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}
153
+
154
+ when Proc
155
+ # return an html-cleaned descriptive string
156
+ h renderer.call(record)
157
+ end
158
+ end
159
+
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
+ end
177
+
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
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,7 @@
1
+ require "recordselect-custom/version"
2
+
3
+ module Recordselect
4
+ module Custom
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Recordselect
2
+ module Custom
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ hello kitty
@@ -0,0 +1,8 @@
1
+ <%
2
+ controller ||= params[:controller]
3
+ record_select_id = record_select_id(controller)
4
+ -%>
5
+ <div class="record-select" id="<%= record_select_id -%>">
6
+ <%= render_record_select '_search.rhtml', :locals => {:controller => controller, :record_select_id => record_select_id} %>
7
+ <%= render_record_select '_list.rhtml', :locals => {:controller => controller, :page => @page, :record_select_id => record_select_id} %>
8
+ </div>
@@ -0,0 +1,34 @@
1
+ <%
2
+ controller ||= params[:controller]
3
+ url_params = {:controller => controller, :action => :select}
4
+
5
+ pagination_url_params = params.merge(:controller => controller, :action => :browse, :search => params[:search], :update => 1) if page.prev? || page.next?
6
+ -%>
7
+
8
+ <ol>
9
+ <li class="found">
10
+ <%= rs_("%d %s #{t("recordselect.found")}", page.pager.count, @count_label || record_select_config.model.to_s.pluralize.titleize.downcase) %>
11
+ </li>
12
+ <% if page.prev? -%>
13
+ <%- prev_url = url_for(pagination_url_params.merge(:page => page.prev.number, :escape => false)) %>
14
+ <li class="pagination previous">
15
+ <%- previous_text = image_tag("record_select/previous.gif", :alt => rs_("#{t("recordselect.previous")}")) + " " + rs_("#{t("recordselect.previous")} %d", page.pager.per_page) %>
16
+ <%- previous_text = "<div>#{previous_text}</div>" %>
17
+ <%= link_to_function previous_text, {:href => prev_url} %>
18
+ </li>
19
+ <% end -%>
20
+ <% page.items.each do |record| -%>
21
+ <li class="record <%= cycle "odd", "even" %>" id="rs<%= record.id -%>">
22
+ <% url = url_for(url_params.merge(:id => record.id, :escape => false)) -%>
23
+ <%= link_to_function render_record_from_config(record), "jQuery(this).recordSelect_notify(); jQuery(this).toggleClass(\"selected\")" %>
24
+ </li>
25
+ <% end -%>
26
+ <% if page.next? -%>
27
+ <%- next_url = url_for(pagination_url_params.merge(:page => page.next.number, :escape => false)) %>
28
+ <li class="pagination next">
29
+ <%- next_text = rs_("#{t("recordselect.next")} %d", page.pager.per_page) + " " + image_tag("record_select/next.gif", :alt => rs_("#{t("recordselect.next")}")) %>
30
+ <%- next_text = "<div>#{next_text}</div>" %>
31
+ <%= link_to_function next_text, {:href => next_url} %>
32
+ </li>
33
+ <% end -%>
34
+ </ol>
@@ -0,0 +1,28 @@
1
+ <% search_url = url_for(params.merge(:controller => controller, :action => :browse, :page => 1, :update => 1, :escape => false)) -%>
2
+ <%# form_remote_tag :url => search_url, :method => :get,
3
+ # :html => { :action => search_url, :id => record_select_search_id } do
4
+ # %>
5
+ <% form_remote_tag :url => search_url, :method => :get, :html => { :action => search_url, :id => record_select_search_id } do %>
6
+ <%= text_field_tag 'search', params[:search], :autocomplete => 'off', :class => 'text-input' %>
7
+ <% end %>
8
+
9
+ <script type="text/javascript">
10
+ //<![CDATA[
11
+ var record_select_search = jQuery(<%= ("#" + record_select_search_id).to_json %>);
12
+ if(record_select_search[0]) {
13
+ record_select_search[0].onsubmit = function(_this) {
14
+ _thisfrm = record_select_search;
15
+ _this.ajaxGetSearh = jQuery.ajax({
16
+ url: '<%=h search_url -%>',
17
+ data: _thisfrm.serialize(),
18
+ success: function(data) {
19
+ eval(data);
20
+ _this.highlightFirst();
21
+ }
22
+ });
23
+
24
+ return false;
25
+ };
26
+ };
27
+ //]]>
28
+ </script>
@@ -0,0 +1,4 @@
1
+ page.assign "data", render_record_select('_list.rhtml', :locals => {:page => @page})
2
+ page.assign "elem", page.literal("jQuery('##{record_select_id} ol')")
3
+
4
+ page << %|if (elem) {jQuery(data).replaceAll('##{record_select_id} ol')}|
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'recordselect-custom/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "recordselect-custom"
8
+ gem.version = Recordselect::Custom::VERSION
9
+ gem.authors = ["lion"]
10
+ gem.email = ["lion3139@gmail.com"]
11
+ gem.description = %q{just for test}
12
+ gem.summary = %q{just for test}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class RecordselectTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ ['stylesheets', 'images', 'javascripts'].each do |asset_type|
2
+ public_dir = File.join(RAILS_ROOT, 'public', asset_type, 'record_select')
3
+ FileUtils.rm_r public_dir
4
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recordselect-custom
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - lion
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2014-12-17 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: just for test
23
+ email:
24
+ - lion3139@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - CHANGELOG
34
+ - Gemfile
35
+ - LICENSE.txt
36
+ - MIT-LICENSE
37
+ - README
38
+ - README.md
39
+ - Rakefile
40
+ - app/helpers/record_select_helper.rb
41
+ - app/views/record_select/_browse.html.erb
42
+ - app/views/record_select/_list.html.erb
43
+ - app/views/record_select/_search.html.erb
44
+ - app/views/record_select/browse.js.rjs
45
+ - assets/images/cross.gif
46
+ - assets/images/next.gif
47
+ - assets/images/previous.gif
48
+ - assets/javascripts/record_select.js
49
+ - assets/stylesheets/record_select.css
50
+ - config/locales/en.yml
51
+ - config/locales/zh.yml
52
+ - init.rb
53
+ - install.rb
54
+ - lib/extensions/active_record.rb
55
+ - lib/localization.rb
56
+ - lib/record_select.rb
57
+ - lib/record_select/actions.rb
58
+ - lib/record_select/conditions.rb
59
+ - lib/record_select/config.rb
60
+ - lib/record_select/form_builder.rb
61
+ - lib/record_select/helpers.rb
62
+ - lib/recordselect-custom.rb
63
+ - lib/recordselect-custom/version.rb
64
+ - lib/views/_browse.html
65
+ - lib/views/_browse.rhtml
66
+ - lib/views/_list.rhtml
67
+ - lib/views/_search.rhtml
68
+ - lib/views/browse.rjs
69
+ - recordselect-custom.gemspec
70
+ - test/recordselect_test.rb
71
+ - uninstall.rb
72
+ has_rdoc: true
73
+ homepage: ""
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.9.5
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: just for test
106
+ test_files:
107
+ - test/recordselect_test.rb