simple_form-polymorphic_associations 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
+ SHA256:
3
+ metadata.gz: e98a1dc07072ac897bf632c74242327a560fab12ded9bdc3d4652aa953af9599
4
+ data.tar.gz: 59cfafaa342ddcbf322c28670c62eab39340f04cc6bac88d4a71484aba68d2bf
5
+ SHA512:
6
+ metadata.gz: 37f1f189f4532f767d1d8d756d2bf0a5a700fb65dc35f44f3a2cba1832fcfb649992cec72f6cb540c70c07cad8d5e1410052a323d5c3087b03d6246d7aed1c39
7
+ data.tar.gz: 11eb85e005ed17ac0bc8c59d13882fc987146a3b5b0bb1af9c269edfcc8f56acf78d46b2ae9112129b137b819e799587a1452eba2bd289629389035cc0b1b008
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Roberto Vasquez Angel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "bundler/setup"
5
+ rescue LoadError
6
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
7
+ end
8
+
9
+ require "rdoc/task"
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = "rdoc"
13
+ rdoc.title = "Simple Form - Polymorphic Associations"
14
+ rdoc.options << "--line-numbers"
15
+ rdoc.rdoc_files.include("README.rdoc")
16
+ rdoc.rdoc_files.include("lib/**/*.rb")
17
+ end
18
+
19
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1 @@
1
+ //= require simple_form-polymorphic_associations/application
@@ -0,0 +1,7 @@
1
+ $ ->
2
+ $('.polymorphic-association-resource-select').select2 ajax:
3
+ url: ->
4
+ klass = $(@.siblings('.polymorphic-association-class-select')[0]).val()
5
+ $(@).parent().find(".polymorphic-association-autocomplete-link[data-class='#{klass}']").attr('href')
6
+ dataType: 'json',
7
+ delay: 500
@@ -0,0 +1,26 @@
1
+ module SimpleFormPolymorphicAssociations
2
+ module Controller
3
+ # Example:
4
+ #
5
+ # # app/controllers/people_controller.rb
6
+ # class PeopleController < ApplicationController
7
+ # include SimpleFormPolymorphicAssociations::AutocompleteConcern
8
+ # end
9
+ #
10
+ module AutocompleteConcern
11
+ extend ActiveSupport::Concern
12
+
13
+ def autocomplete
14
+ @collection = if params[:term].present?
15
+ load_collection_scope.autocomplete(params[:term])
16
+ else
17
+ []
18
+ end
19
+
20
+ respond_to do |format|
21
+ format.json { render json: { results: @collection.map { |q| q.as_autocomplete_json } } }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ module SimpleFormPolymorphicAssociations
2
+ module Model
3
+ # Example:
4
+ #
5
+ # # app/models/person.rb
6
+ # class Person < ActiveRecord::Base
7
+ # include SimpleFormPolymorphicAssociations::AutocompleteConcern
8
+ # autocomplete scope: ->(matcher) { where("people.firstname LIKE :term", term: "%#{matcher.downcase}%") }, id_method: :id, text_method: :human
9
+ # end
10
+ #
11
+ module AutocompleteConcern
12
+ extend ActiveSupport::Concern
13
+
14
+ class_methods do
15
+ def autocomplete(options)
16
+ self.send(:scope, :autocomplete, options[:scope])
17
+ @autocomplete_options = options
18
+ end
19
+
20
+ def autocomplete_options
21
+ @autocomplete_options
22
+ end
23
+ end
24
+
25
+ def as_autocomplete_json
26
+ { id: send(self.class.autocomplete_options[:id_method]), text: send(self.class.autocomplete_options[:text_method]) }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PolymorphicAssociationInput < SimpleForm::Inputs::Base
4
+ # Example:
5
+ #
6
+ # In this example an achievement belongs to an achiever (polymorphic). To make this
7
+ # work you will need four things:
8
+ #
9
+ # 1. A controller action for models that are candidates for the polymorphic association.
10
+ # 2. Routing for these controllers
11
+ # 3. Properly configured models that the controller will query.
12
+ # 4. The form input in the form of the model on the belongs_to side of the polymorphic association.
13
+ #
14
+ # First, setup the controller:
15
+ #
16
+ # # app/controllers/people_controller.rb
17
+ # class PeopleController < ApplicationController
18
+ # include SimpleFormPolymorphicAssociations::AutocompleteConcern
19
+ # end
20
+ #
21
+ # Then add the routing:
22
+ #
23
+ # # config/routes.rb
24
+ # Rails.application.routes.draw do
25
+ # resources :people do
26
+ # get :autocomplete, on: :collection
27
+ # end
28
+ # end
29
+ #
30
+ # Configure your model to support autocompletion:
31
+ #
32
+ # # app/models/person.rb
33
+ # class Person < ActiveRecord::Base
34
+ # include SimpleFormPolymorphicAssociations::AutocompleteConcern
35
+ # autocomplete scope: ->(matcher) { where("people.firstname LIKE :term", term: "%#{matcher.downcase}%") }, id_method: :id, text_method: :human
36
+ # end
37
+ #
38
+ # Yo will need to repeat these first three steps for every model that the can
39
+ # be attached to the polymorphic association.
40
+ #
41
+ # Finally add the input field to the form:
42
+ #
43
+ # # app/views/achievements/form.html.haml
44
+ # = form.input :achiever, as: :polymorphic_association, classes: { Person => url_for([:autocomplete, Person]), SomeOtherModel => url_for([:autocomplete, SomeOtherModel]) }
45
+ #
46
+ def input(wrapper_options = nil)
47
+ ActiveSupport::SafeBuffer.new.tap do |o|
48
+ collection = options[:classes].keys.collect { |c| [c.model_name.human, c] }
49
+ label_method = :to_s
50
+ value_method = :to_s
51
+ o << @builder.select("#{attribute_name}_type", collection, { include_blank: true }, { class: 'form-control select required polymorphic-association-class-select' })
52
+ options[:classes].each do |klass, url|
53
+ o << "<a class=\"polymorphic-association-autocomplete-link\" data-class=\"#{klass}\" href=\"#{url}\" ></a>".html_safe
54
+ end
55
+ o << @builder.select("#{attribute_name}_id", [], {}, { class: 'form-control select required polymorphic-association-resource-select' })
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.config.assets.precompile += %w( simple_form-polymorphic_associations.js )
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ require "simple_form"
3
+ require "simple_form-polymorphic_associations/engine"
4
+
5
+ module SimpleFormPolymorphicAssociations
6
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleFormPolymorphicAssociations
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace SimpleFormPolymorphicAssociations
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleFormPolymorphicAssociations
4
+ VERSION = "0.0.1"
5
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_form-polymorphic_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Vasquez Angel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simple_form
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - roberto@vasquez-angel.de
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - Rakefile
36
+ - app/assets/javascripts/simple_form-polymorphic_associations.js
37
+ - app/assets/javascripts/simple_form-polymorphic_associations/application.js
38
+ - app/assets/javascripts/simple_form-polymorphic_associations/application/autocomplete.js.coffee
39
+ - app/concerns/simple_form_polymorphic_associations/controller/autocomplete_concern.rb
40
+ - app/concerns/simple_form_polymorphic_associations/model/autocomplete_concern.rb
41
+ - app/inputs/polymorphic_association_input.rb
42
+ - config/initializers/assets.rb
43
+ - lib/simple_form-polymorphic_associations.rb
44
+ - lib/simple_form-polymorphic_associations/engine.rb
45
+ - lib/simple_form-polymorphic_associations/version.rb
46
+ homepage:
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.7.8
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Simple Form polymorphic associations.
70
+ test_files: []