polymorphic_select 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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in polymorphic_select.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ require "polymorphic_select/version"
2
+ require "polymorphic_select/instance_tag_ext"
3
+ require "polymorphic_select/form_builder_ext"
4
+ require "polymorphic_select/helper"
5
+ require "polymorphic_select/multiparameter_assignment_ext"
6
+
7
+ module PolymorphicSelect #:nodoc:
8
+
9
+ class Engine < ::Rails::Engine
10
+ initializer "polymorphic_select.initialize" do
11
+ config.to_prepare do
12
+ ActiveRecord::Base.send :include, MultiparameterAssignmentExt
13
+ ActionView::Helpers::InstanceTag.send :include, InstanceTagExt
14
+ ActionView::Helpers::FormBuilder.send :include, FormBuilderExt
15
+ ActionView::Base.send :include, PolymorphicSelectHelper
16
+ end
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,12 @@
1
+ module PolymorphicSelect
2
+ module FormBuilderExt
3
+ extend ActiveSupport::Concern
4
+
5
+ def polymorphic_select(method, choices, options = {}, html_options = {})
6
+ @template.polymorphic_select(@object_name, method, choices, objectify_options(options), html_options)
7
+ end
8
+ def grouped_polymorphic_select(method, choices, options = {}, html_options = {})
9
+ @template.grouped_polymorphic_select(@object_name, method, choices, objectify_options(options), html_options)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module PolymorphicSelect
2
+ module PolymorphicSelectHelper
3
+ def polymorphic_select(object_name, method, choices, options = {}, html_options = {})
4
+ ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_polymorphic_select_tag(choices, options, html_options)
5
+ end
6
+ def grouped_polymorphic_select(object_name, method, choices, options = {}, html_options = {})
7
+ ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_grouped_polymorphic_select_tag(choices, options, html_options)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ require "polymorphic_select/polymorphic_selector"
2
+
3
+ module PolymorphicSelect
4
+ module InstanceTagExt
5
+ extend ActiveSupport::Concern
6
+
7
+ def to_polymorphic_select_tag(choices, options = {}, html_options = {})
8
+ polymorphic_selector(choices, options, html_options).select_polymorph.html_safe
9
+ end
10
+ def to_grouped_polymorphic_select_tag(choices, options = {}, html_options = {})
11
+ polymorphic_selector(choices, options, html_options).select_polymorph.html_safe
12
+ end
13
+ protected
14
+ def polymorphic_selector(choices, options, html_options)
15
+ polymorph = value(object)
16
+ @auto_index ||= nil
17
+
18
+ options = options.dup
19
+ options[:field_name] = @method_name
20
+ options[:prefix] ||= @object_name
21
+ options[:index] = @auto_index if @auto_index && !options.has_key?(:index)
22
+
23
+ PolymorphicSelector.new(polymorph, choices, options, html_options)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # TODO: Write code to inject into multiparameter_assignment process
2
+
3
+ # This code needs to determine what multiparameter assignments are
4
+ # actually related to assocations rather than aggregations. If they
5
+ # are associations then it needs to extract them from the list of
6
+ # pairs and handle them separately.
7
+
8
+ module PolymorphicSelect
9
+ module MultiparameterAssignmentExt
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ alias_method :read_value_from_parameter_without_polymorphs, :read_value_from_parameter
14
+ alias_method :read_value_from_parameter, :read_value_from_parameter_with_polymorphs
15
+ end
16
+
17
+ private
18
+ def read_value_from_parameter_with_polymorphs(name, values_hash_from_param)
19
+ begin
20
+ read_value_from_parameter_without_polymorphs(name, values_hash_from_param)
21
+ rescue NoMethodError => e
22
+ reflection = self.class.reflect_on_association(name.to_sym)
23
+ raise e if reflection.nil? || reflection.options[:polymorphic] != true
24
+ read_polymorphic_parameter_value(name, values_hash_from_param)
25
+ end
26
+ end
27
+
28
+ def read_polymorphic_parameter_value(name, values_hash_from_param)
29
+ polymorphic_type, polymorphic_id = values_hash_from_param[1].split(':', 2)
30
+ polymorphic_klass = Object.const_get(polymorphic_type) rescue nil
31
+ return if polymorphic_klass.nil?
32
+
33
+ polymorphic_klass.find(polymorphic_id) rescue nil
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ require 'action_view/helpers'
2
+
3
+ module PolymorphicSelect
4
+ class PolymorphicSelector
5
+ include ActionView::Helpers::TagHelper
6
+ include ActionView::Helpers::FormOptionsHelper
7
+
8
+ def initialize(polymorph, choices, options = {}, html_options = {})
9
+ @polymorph = polymorph
10
+ @choices = choices
11
+ @options = options.dup
12
+ @html_options = html_options.dup
13
+ end
14
+
15
+ def select_polymorph
16
+ build_options_and_select(selected_value, @choices)
17
+ end
18
+
19
+ protected
20
+ def selected_value
21
+ return if @polymorph.nil?
22
+ "#{@polymorph.class.model_name}:#{@polymorph.id}"
23
+ end
24
+ def build_options_and_select(selected, choices, options = {})
25
+ build_select(build_options(selected, choices, options))
26
+ end
27
+ def build_options(selected, choices, options = {})
28
+ options_for_select(choices, selected) + "\n"
29
+ end
30
+ def build_select(select_options_as_html)
31
+ select_options = {
32
+ :id => input_id,
33
+ :name => input_name
34
+ }.merge(@html_options)
35
+ select_options.merge!(:disabled => 'disabled') if @options[:disabled]
36
+
37
+ select_html = "\n"
38
+ select_html << content_tag(:option, '', :value => '') + "\n" if @options[:include_blank]
39
+ select_html << select_options_as_html
40
+
41
+ (content_tag(:select, select_html.html_safe, select_options) + "\n").html_safe
42
+ end
43
+ def input_name
44
+ prefix = @options[:prefix] || ''
45
+ prefix += "[#{@options[:index]}]" if @options.has_key?(:index)
46
+
47
+ field_name = @options[:field_name] || ''
48
+ field_name += "(1)"
49
+
50
+ @options[:discard_type] ? prefix : "#{prefix}[#{field_name}]"
51
+ end
52
+ def input_id
53
+ input_name.gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '')
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module PolymorphicSelect
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "polymorphic_select/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "polymorphic_select"
7
+ s.version = PolymorphicSelect::VERSION
8
+ s.authors = ["Geoff Garside"]
9
+ s.email = ["geoff@geoffgarside.co.uk"]
10
+ s.homepage = "https://github.com/geoffgarside/polymorphic_select"
11
+ s.summary = %q{Rails 3.1 Polymorphic Select helpers}
12
+ s.description = %q{Rails 3.1 Polymorphic Select view helpers and model support}
13
+
14
+ s.rubyforge_project = "polymorphic_select"
15
+
16
+ s.add_dependency "railties", "~> 3.1.0"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polymorphic_select
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
+ - Geoff Garside
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 3
29
+ - 1
30
+ - 0
31
+ version: 3.1.0
32
+ requirement: *id001
33
+ prerelease: false
34
+ name: railties
35
+ type: :runtime
36
+ description: Rails 3.1 Polymorphic Select view helpers and model support
37
+ email:
38
+ - geoff@geoffgarside.co.uk
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - Rakefile
49
+ - lib/polymorphic_select.rb
50
+ - lib/polymorphic_select/form_builder_ext.rb
51
+ - lib/polymorphic_select/helper.rb
52
+ - lib/polymorphic_select/instance_tag_ext.rb
53
+ - lib/polymorphic_select/multiparameter_assignment_ext.rb
54
+ - lib/polymorphic_select/polymorphic_selector.rb
55
+ - lib/polymorphic_select/version.rb
56
+ - polymorphic_select.gemspec
57
+ homepage: https://github.com/geoffgarside/polymorphic_select
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: polymorphic_select
86
+ rubygems_version: 1.8.10
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Rails 3.1 Polymorphic Select helpers
90
+ test_files: []
91
+