dynamic_selectable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b7f85c87b48e34c7b50514a220866f28c90d1e8
4
+ data.tar.gz: ec0448eeef9049b09ffbc00dd2a11fb2aee80345
5
+ SHA512:
6
+ metadata.gz: ec3da87a01ef946068c95835cd7fce47b913b51599edfe46876591b001ec8d00c367dfb6af7aaa01949f79dce145d9ca8fa1fd60d0ed7225322892ed5d39a196
7
+ data.tar.gz: 056e1406e19753391bc3583634a0bee4e0db5c70433c50c4ef149202f279267446321014a80864eddccacb99c3feb6b4ad02c8ee9b44b0eb53a93236514c2c63
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dynamic_selectable.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 Moorem Technologies
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,179 @@
1
+ # DynamicSelectable
2
+
3
+ DynamicSelectable is a [Rails](http://github.com/rails/rails) gem that allows you to easily create `collection_select` fields with results that dynamically populate a related field. The dynamic population occurs with the help of the jQuery library [*jquery-dynamic-selectable*](http://railsguides.net/cascading-selects-with-ajax-in-rails/) written by [Andrey Koleshko](http://railsguides.net/about-author/).
4
+
5
+ How about a use case? Let's say your application allowed users to look up parts for their vehicle. A user selecting their car's model might include the following:
6
+
7
+ 1. User selects the `Make` of their vehicle
8
+ 2. The `Model` field is populated with models of the selected `Make`
9
+ 3. User selects the `Model` of their vehicle
10
+
11
+ DynamicSelectable makes the above easy to do by providing a generator for your dynamic `collection_select` along with a new `FormHelper` method called `dynamic_collection_select`.
12
+
13
+ To see this gem in action, you can check out the sample application [here](https://github.com/mattantonelli/dynamic-selectable-test).
14
+
15
+ ## Updating from 0.0.2
16
+
17
+ ***Warning:*** *If you are updating from `0.0.2` you will need to re-generate any content created with* `rails generate dynamic_selectable:select`.
18
+
19
+ ## Installation
20
+
21
+ Add the following line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'dynamic_selectable', git: 'https://github.com/moorem/dynamic_selectable.git'
25
+ ```
26
+
27
+ Install the gem by running `bundle install`.
28
+
29
+ Run the gem's install generator:
30
+
31
+ ```
32
+ $ rails generate dynamic_selectable:install
33
+ ```
34
+
35
+ If you are using [Devise](https://github.com/plataformatec/devise) for user authentication, you will want to skip authentication in the newly generated `app/controllers/select/select_controller.rb`:
36
+
37
+ ```ruby
38
+ class DynamicSelectable::SelectController < ApplicationController
39
+ # skip_before_action :authenticate_user!
40
+ end
41
+ ```
42
+
43
+ Finally, you'll want to add the following require to your `application.js`:
44
+
45
+ ```javascript
46
+ //= require jquery-dynamic-selectable
47
+ ```
48
+
49
+ Now that you have DynamicSelectable installed, you're ready to start creating some `dynamic_collection_select` fields in your application!
50
+
51
+ ## Usage
52
+
53
+ The first step to creating a `dynamic_collection_select` is to generate a controller and route for it. Using the vehicle example from above, your `Make` and `Model` models might look like the following:
54
+
55
+ ```ruby
56
+ # == Schema Information
57
+ #
58
+ # Table name: makes
59
+ #
60
+ # id :integer not null, primary key
61
+ # name :string
62
+ # created_at :datetime not null
63
+ # updated_at :datetime not null
64
+ #
65
+
66
+ class Make < ActiveRecord::Base
67
+ end
68
+ ```
69
+
70
+ ```ruby
71
+ # == Schema Information
72
+ #
73
+ # Table name: models
74
+ #
75
+ # id :integer not null, primary key
76
+ # name :string
77
+ # make_id :integer
78
+ # created_at :datetime not null
79
+ # updated_at :datetime not null
80
+ #
81
+
82
+ class Model < ActiveRecord::Base
83
+ belongs_to :make
84
+ end
85
+ ```
86
+
87
+ To generate the controller and route necessary for your selection of `Make` to dynamically populate your selection of `Model`, run the following:
88
+
89
+ ```
90
+ # Usage: rails generate dynamic_selectable:select parent child value_method text_method [sort_col:asc/desc]
91
+ $ rails generate dynamic_selectable:select make model id name name:asc
92
+ ```
93
+
94
+ Now you just need to build your form. The method `dynamic_collection_select` is very similar to [collection_select](http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select), but contains the new `dynamic_model` parameter. This parameter is a symbol representing the child model whose collection will be populated based on the value selected in this parent field.
95
+
96
+ If you would like to submit the value of the `dynamic_collection_select` with the form, just add `{ submit_with_form: true }` to the `options` hash.
97
+
98
+
99
+ ```ruby
100
+ def dynamic_collection_select(object, method, dynamic_model, collection, value_method, text_method,
101
+ options = {}, html_options = {})
102
+ ```
103
+
104
+ A very simple form would look something like this:
105
+
106
+ ```html+erb
107
+ <%= form_for(@vehicle) do |f| %>
108
+ <div>
109
+ <%= label_tag :make_id %>
110
+ <%= dynamic_collection_select :vehicle, :make_id, :model, Make.all, :id, :name,
111
+ { include_blank: true }, {} %>
112
+ </div>
113
+
114
+ <div>
115
+ <%= f.label :model_id %>
116
+ <%= f.collection_select :model_id, [], :id, :name, {}, {} %>
117
+ </div>
118
+
119
+ <%# ... %>
120
+ <% end %>
121
+ ```
122
+
123
+ That's it. Happy selecting!
124
+
125
+ ## Overriding data attributes
126
+
127
+ By default, DynamicSelectable uses the `object` and `method` parameters of `dynamic_collection_select` to generate data attributes used by the gem's Javascript. Depending on your model, or if you are creating a form with a gem like [Ransack](https://github.com/activerecord-hackery/ransack), the values you provide for these parameters could prevent DynamicSelectable from generating the necessary data attributes properly. To resolve this, you can manually specify these attributes in a `data` hash within your tag's `html_options` hash. For example:
128
+
129
+ ```html+erb
130
+ <%= dynamic_collection_select :q, :make_id_eq, :model, Make.all, :id, :name,
131
+ {}, { data: { dynamic_selectable_url: '/dynamic_selectable/makes/:make_id/models',
132
+ dynamic_selectable_target: '#q_model_id_eq' } } %>
133
+ ```
134
+
135
+ The value for `dynamic_selectable_url` can be found in your routes:
136
+
137
+ ```
138
+ $ rake routes | grep dynamic
139
+ dynamic_selectable_make_models GET /dynamic_selectable/makes/:make_id/models(.:format) dynamic_selectable/make_models#index
140
+ ```
141
+
142
+ and the value for `dynamic_selectable_target` is generated with your form. This can be found by inspecting the child element in your web browser.
143
+
144
+ ## Removing generated content
145
+
146
+ #### Remove a generated controller/route
147
+
148
+ ```
149
+ $ rm app/controllers/dynamic_selectable/models_controller.rb
150
+ ```
151
+
152
+ and remove the related line from your `routes.rb`:
153
+
154
+ ```ruby
155
+ get ':make_id/models', to: 'models#index', as: :models
156
+ ```
157
+
158
+ #### Complete uninstallation
159
+
160
+ ```bash
161
+ $ rm -rf app/controllers/dynamic_selectable
162
+ ```
163
+
164
+ and remove the following block from your `routes.rb`:
165
+
166
+ ```ruby
167
+ namespace :dynamic_selectable do
168
+ # ...
169
+ end
170
+ ```
171
+
172
+ ## Contributing
173
+
174
+ 1. Fork it ( https://github.com/moorem/dynamic_selectable/fork )
175
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
176
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
177
+ 4. Push to the branch (`git push origin my-new-feature`)
178
+ 5. Create a new Pull Request
179
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dynamic_selectable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dynamic_selectable'
8
+ spec.version = DynamicSelectable::VERSION
9
+ spec.authors = ['Moorem Technologies']
10
+ spec.email = ['mooremtechnologies@gmail.com']
11
+ spec.summary = %q{Allows for easy dynamic population of cascading collection_select fields.}
12
+ spec.description = 'DynamicSelectable will allow you to easily create collection_select fields
13
+ with results that dynamically populate a related field.'
14
+ spec.homepage = 'https://github.com/moorem/dynamic_selectable'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+
25
+ spec.add_runtime_dependency 'coffee-script', '~> 2.3'
26
+ spec.add_runtime_dependency 'rails', '>= 4.1', '< 6.0'
27
+ end
@@ -0,0 +1,7 @@
1
+ require 'dynamic_selectable/version'
2
+ require 'dynamic_selectable/rails/engine'
3
+ require 'dynamic_selectable/action_view/helpers/dynamic_form_options_helper'
4
+ require 'dynamic_selectable/dynamic_collection_select' if defined?(Rails)
5
+
6
+ module DynamicSelectable
7
+ end
@@ -0,0 +1,30 @@
1
+ module DynamicSelectable
2
+ module ActionView
3
+ module Helpers
4
+ module DynamicFormOptionsHelper
5
+ def dynamic_collection_select(object, method, dynamic_model, collection, value_method, text_method,
6
+ options = {}, html_options = {})
7
+ select_url = "dynamic_selectable_#{method.to_s.sub(/_id$/, '')}_#{dynamic_model.to_s.pluralize}_path"
8
+ parent_id = ":#{method}"
9
+ select_target = "##{object}_#{dynamic_model}_id"
10
+
11
+ data = html_options.fetch(:data, {})
12
+ url, target = data.delete(:dynamic_selectable_url), data.delete(:dynamic_selectable_target)
13
+
14
+ data_options = { data: { dynamic_selectable_url: url || send(select_url, parent_id),
15
+ dynamic_selectable_target: target || select_target } }
16
+
17
+ data_options[:data].merge!(data)
18
+
19
+ if options[:submit_with_form] == true
20
+ submit_object = object
21
+ submit_method = method
22
+ end
23
+
24
+ collection_select(submit_object, submit_method, collection, value_method, text_method,
25
+ options, html_options.merge(data_options))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ module DynamicSelectable
2
+ class Railtie < Rails::Railtie
3
+ initializer 'dynamic_selectable.configure_form_options_helpers' do |app|
4
+ ActiveSupport.on_load :action_view do
5
+ include DynamicSelectable::ActionView::Helpers::DynamicFormOptionsHelper
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module DynamicSelectable
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module DynamicSelectable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ module DynamicSelectable
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_route_namespace
6
+ inject_into_file 'config/routes.rb',
7
+ " namespace :dynamic_selectable do\n end\n\n",
8
+ after: "Rails.application.routes.draw do\n"
9
+ end
10
+
11
+ def create_parent_controller
12
+ copy_file 'select_controller.rb', 'app/controllers/dynamic_selectable/select_controller.rb'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ module DynamicSelectable
2
+ class SelectGenerator < Rails::Generators::Base
3
+ argument :attributes, :type => :array, :default => [],
4
+ :banner => "parent child value_method text_method [sort_col:asc/desc]"
5
+
6
+ def create_route
7
+ inject_into_file 'config/routes.rb', route(*attributes.first(2)), after: "namespace :dynamic_selectable do\n"
8
+ end
9
+
10
+ def create_controller_file
11
+ create_file controller_path(*attributes.first(2)), controller_body(*attributes)
12
+ end
13
+
14
+ private
15
+ def route(parent, child)
16
+ children = child.pluralize
17
+ " get '#{parent.pluralize}/:#{parent}_id/#{children}', to: '#{parent}_#{children}#index', as: :#{parent}_#{children}\n"
18
+ end
19
+
20
+ def controller_path(parent, child)
21
+ "app/controllers/dynamic_selectable/#{parent}_#{child.pluralize.underscore}_controller.rb"
22
+ end
23
+
24
+ def controller_body(parent, child, val, text, sort)
25
+ children = child.pluralize
26
+ parent_class = parent.titleize.gsub(' ', '')
27
+ select_class = children.titleize.gsub(' ', '')
28
+ child_class = child.titleize.gsub(' ', '')
29
+ order = sort.present? ? ".order('#{sort.gsub(':', ' ')}')" : ''
30
+
31
+ <<-END
32
+ module DynamicSelectable
33
+ class #{parent_class}#{select_class}Controller < SelectController
34
+ def index
35
+ #{children} = #{child_class}.where(#{parent}_id: params[:#{parent}_id]).select('#{val}, #{text}')#{order}
36
+ render json: #{children}
37
+ end
38
+ end
39
+ end
40
+ END
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ class DynamicSelectable::SelectController < ApplicationController
2
+ # skip_before_action :authenticate_user!
3
+ end
@@ -0,0 +1,35 @@
1
+ $.fn.extend
2
+ dynamicSelectable: ->
3
+ $(@).each (i, el) ->
4
+ new DynamicSelectable($(el))
5
+
6
+ class DynamicSelectable
7
+ constructor: ($select) ->
8
+ @init($select)
9
+
10
+ init: ($select) ->
11
+ @urlTemplate = $select.data('dynamicSelectableUrl')
12
+ @$targetSelect = $($select.data('dynamicSelectableTarget'))
13
+ $select.on 'change', =>
14
+ @clearTarget()
15
+ url = @constructUrl($select.val())
16
+ if url
17
+ $.getJSON url, (data) =>
18
+ $.each data, (index, el) =>
19
+ @$targetSelect.append "<option value='#{el.id}'>#{el.name}</option>"
20
+ @reinitializeTarget()
21
+ else
22
+ @reinitializeTarget()
23
+
24
+ reinitializeTarget: ->
25
+ @$targetSelect.trigger("change")
26
+
27
+ clearTarget: ->
28
+ @$targetSelect.html('<option></option>')
29
+
30
+ constructUrl: (id) ->
31
+ if id && id != ''
32
+ @urlTemplate.replace(/:.+_id/, id)
33
+
34
+ $ ->
35
+ $('select[data-dynamic-selectable-url][data-dynamic-selectable-target]').dynamicSelectable()
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynamic_selectable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Moorem Technologies
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coffee-script
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.1'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '6.0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '4.1'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '6.0'
75
+ description: |-
76
+ DynamicSelectable will allow you to easily create collection_select fields
77
+ with results that dynamically populate a related field.
78
+ email:
79
+ - mooremtechnologies@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - ".idea/inspectionProfiles/Project_Default.xml"
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - dynamic_selectable.gemspec
91
+ - lib/dynamic_selectable.rb
92
+ - lib/dynamic_selectable/action_view/helpers/dynamic_form_options_helper.rb
93
+ - lib/dynamic_selectable/dynamic_collection_select.rb
94
+ - lib/dynamic_selectable/rails/engine.rb
95
+ - lib/dynamic_selectable/version.rb
96
+ - lib/generators/dynamic_selectable/install_generator.rb
97
+ - lib/generators/dynamic_selectable/select_generator.rb
98
+ - lib/generators/dynamic_selectable/templates/select_controller.rb
99
+ - vendor/assets/javascripts/jquery-dynamic-selectable.coffee
100
+ homepage: https://github.com/moorem/dynamic_selectable
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.6.14
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Allows for easy dynamic population of cascading collection_select fields.
124
+ test_files: []