carmen-country 1.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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e13a40ba9088741f10e7be6b32012be83b10b8d
4
+ data.tar.gz: efb23fabeab8b2525aa2b5fb77279e835e2cc6c2
5
+ SHA512:
6
+ metadata.gz: 4519a20e5f973d8e38f908b896079ef3e724071e56af26af0498cb3c88ce38b6436f7ac9bdf1d657fbb49bfad34329727f6c26fae94201d927d9fdd26a43485a
7
+ data.tar.gz: a349e380dbad21785d122467557d6fbbf977a8e1c0a5dfacaccbefee7ee428003011b2a2779fa88bb25d03c2d3f82f82e8e6a15c43130fa0459d8c8b12138670
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Nilesh Patil and contributors
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.
File without changes
@@ -0,0 +1,18 @@
1
+ require "rubygems"
2
+ require 'bundler/setup'
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rdoc/task'
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Default: run unit tests.'
18
+ task :default => :test
@@ -0,0 +1,19 @@
1
+ require 'carmen'
2
+ require 'carmen/rails/action_view/form_helper'
3
+ require 'carmen/rails/version'
4
+
5
+ module Carmen
6
+ module Rails
7
+ class Railtie < ::Rails::Railtie
8
+ # Add Carmen's locale paths to the Rails backend
9
+ paths = Carmen.i18n_backend.locale_paths.map { |path|
10
+ Dir[path + '**/*.yml']
11
+ }.flatten.compact
12
+ Carmen.i18n_backend = ::I18n
13
+ config.i18n.load_path += paths
14
+
15
+ # Enable fallbacks so that missing translations use the default locale
16
+ config.i18n.fallbacks = true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,254 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormOptionsHelper
4
+
5
+ # Generate select and subregion option tags for the given object and method. A
6
+ # common use of this would be to allow users to select a state subregion within
7
+ # a given country.
8
+ #
9
+ # object - The model object to generate the select for
10
+ # method - The attribute on the object
11
+ # parent_region_or_code - An instance of Carmen::Region or a 2-character
12
+ # country code.
13
+ # options - Other options pertaining to option tag generation. See
14
+ # `region_options_for_select`.
15
+ # html_options - Options to use when generating the select tag- class,
16
+ # id, etc.
17
+ #
18
+ # Uses region_options_for_select to generate the list of option tags.
19
+ #
20
+ # Example:
21
+ #
22
+ # subregion_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
23
+ #
24
+ # Returns an `html_safe` string containing the HTML for a select element.
25
+ def subregion_select(object, method, parent_region_or_code, options={}, html_options={})
26
+ parent_region = determine_parent(parent_region_or_code)
27
+ tag = instance_tag(object, method, self, options)
28
+ tag.to_region_select_tag(parent_region, options, html_options)
29
+ end
30
+
31
+ # Generate select and country option tags for the given object and method. A
32
+ # common use of this would be to allow users to select a state subregion within
33
+ # a given country.
34
+ #
35
+ # object - The model object to generate the select for
36
+ # method - The attribute on the object
37
+ # options - Other options pertaining to option tag generation. See
38
+ # `region_options_for_select`.
39
+ # html_options - Options to use when generating the select tag- class,
40
+ # id, etc.
41
+ #
42
+ # Uses region_options_or_select to generate the list of option tags.
43
+ #
44
+ # Example:
45
+ #
46
+ # country_select(@object, :region, {priority: ['US', 'CA']}, class: 'region')
47
+ #
48
+ # Note that in order to preserve compatibility with various existing
49
+ # libraries, an alternative API is supported but not recommended:
50
+ #
51
+ # country_select(@object, :region, ['US', 'CA'], class: region)
52
+ #
53
+ # Returns an `html_safe` string containing the HTML for a select element.
54
+ def country_select(object, method, priorities_or_options = {}, options_or_html_options = {}, html_options = {})
55
+ if priorities_or_options.is_a? Array
56
+ options = options_or_html_options
57
+ options[:priority] = priorities_or_options
58
+ else
59
+ options = priorities_or_options
60
+ html_options = options_or_html_options
61
+ end
62
+
63
+ tag = instance_tag(object, method, self, options)
64
+ tag.to_region_select_tag(Carmen::World.instance, options, html_options)
65
+ end
66
+
67
+ # Generate option tags for a collection of regions.
68
+ #
69
+ # regions - An array or Carmen::RegionCollection containing Carmen::Regions
70
+ # selected - the code of the region that should be selected
71
+ # options - The hash of options used to customize the output (default: {}):
72
+ #
73
+ # To use priority regions (which are included in a special section at the
74
+ # top of the list), provide an array of region codes at the :priority
75
+ # option:
76
+ #
77
+ # region_options_for_select(@region.subregions, 'US', priority: ['US', 'CA'])
78
+ #
79
+ # Returns an `html_safe` string containing option tags.
80
+ def region_options_for_select(regions, selected=nil, options={})
81
+ options.stringify_keys!
82
+ priority_region_codes = options['priority'] || []
83
+ region_options = ""
84
+
85
+ unless priority_region_codes.empty?
86
+ unless regions.respond_to?(:coded)
87
+ regions = Carmen::RegionCollection.new(regions)
88
+ end
89
+
90
+ priority_regions = priority_region_codes.map do |code|
91
+ region = regions.coded(code)
92
+ [region.name, region.code] if region
93
+ end.compact
94
+ unless priority_regions.empty?
95
+ region_options += options_for_select(priority_regions, selected)
96
+ region_options += "<option disabled>-------------</option>"
97
+
98
+ # If a priority region is selected, don't select it again in the main list.
99
+ # This prevents some browsers from selecting the second occurance of this region,
100
+ # which makes it difficult to select an alternative priority region.
101
+ selected = nil if priority_region_codes.include?(selected)
102
+ end
103
+ end
104
+
105
+ main_options = regions.map { |r| [r.name, r.code] }
106
+ main_options.sort!{|a, b| a.first.to_s <=> b.first.to_s}
107
+ main_options.unshift [options['prompt'], ''] if options['prompt']
108
+
109
+ region_options += options_for_select(main_options, selected)
110
+ region_options.html_safe
111
+ end
112
+
113
+ # Generate select and country option tags with the provided name. A
114
+ # common use of this would be to allow users to select a country name
115
+ # inside a web form.
116
+ #
117
+ # name - The name attribute for the select element.
118
+ # options - Other options pertaining to option tag generation. See
119
+ # `region_options_for_select`.
120
+ # html_options - Options to use when generating the select tag- class,
121
+ # id, etc.
122
+ #
123
+ # Uses region_options_or_select to generate the list of option tags.
124
+ #
125
+ # Example:
126
+ #
127
+ # country_select_tag('country_code', {priority: ['US', 'CA']}, class: 'region')
128
+ #
129
+ # Returns an `html_safe` string containing the HTML for a select element.
130
+ def country_select_tag(name, value, options={})
131
+ subregion_select_tag(name, value, Carmen::World.instance, options)
132
+ end
133
+
134
+ # Generate select and subregion option tags for the given object and method. A
135
+ # common use of this would be to allow users to select a state subregion within
136
+ # a given country.
137
+ #
138
+ # name - The name attribute for the select element.
139
+ # parent_region_or_code - An instance of Carmen::Region or a 2-character
140
+ # country code.
141
+ # options - Other options pertaining to option tag generation. See
142
+ # `region_options_for_select`.
143
+ # html_options - Options to use when generating the select tag- class,
144
+ # id, etc.
145
+ #
146
+ # Uses region_options_or_select to generate the list of option tags.
147
+ #
148
+ # Example:
149
+ #
150
+ # subregion_select_tag('state_code', 'US', {priority: ['US', 'CA']}, class: 'region')
151
+ #
152
+ # Returns an `html_safe` string containing the HTML for a select element.
153
+ def subregion_select_tag(name, value, parent_region_or_code, options = {}, html_options = {})
154
+ options.stringify_keys!
155
+ parent_region = determine_parent(parent_region_or_code)
156
+ opts = region_options_for_select(parent_region.subregions, value, options)
157
+ html_options = {"name" => name,
158
+ "id" => sanitize_to_id(name)}.update(html_options.stringify_keys)
159
+ content_tag(:select, opts, html_options)
160
+ end
161
+
162
+ private
163
+
164
+ def instance_tag(object_name, method_name, template_object, options = {})
165
+ if Rails::VERSION::MAJOR == 3
166
+ InstanceTag.new(object_name, method_name, template_object, options.delete(:object))
167
+ else
168
+ ActionView::Helpers::Tags::Base.new(object_name, method_name, template_object, options || {})
169
+ end
170
+ end
171
+
172
+
173
+ def determine_parent(parent_region_or_code)
174
+ case parent_region_or_code
175
+ when String
176
+ Carmen::Country.coded(parent_region_or_code)
177
+ when Array
178
+ parent_region_or_code.inject(Carmen::World.instance) { |parent, next_code|
179
+ parent.subregions.coded(next_code)
180
+ }
181
+ else
182
+ parent_region_or_code
183
+ end
184
+ end
185
+ end
186
+
187
+ if Rails::VERSION::MAJOR == 3
188
+ class InstanceTag
189
+ def to_region_select_tag(parent_region, options = {}, html_options = {})
190
+ html_options = html_options.stringify_keys
191
+ add_default_name_and_id(html_options)
192
+ priority_regions = options[:priority] || []
193
+ value = options[:selected] ? options[:selected] : value(object)
194
+ opts = add_options(region_options_for_select(parent_region.subregions, value, :priority => priority_regions), options, value)
195
+ content_tag("select", opts, html_options)
196
+ end
197
+ end
198
+ end
199
+
200
+ if Rails::VERSION::MAJOR == 4
201
+ module Tags
202
+ class Base
203
+ def to_region_select_tag(parent_region, options = {}, html_options = {})
204
+ html_options = html_options.stringify_keys
205
+ add_default_name_and_id(html_options)
206
+ options[:include_blank] ||= true unless options[:prompt] || select_not_required?(html_options)
207
+
208
+ value = options[:selected] ? options[:selected] : value(object)
209
+ priority_regions = options[:priority] || []
210
+ opts = add_options(region_options_for_select(parent_region.subregions, value,
211
+ :priority => priority_regions),
212
+ options, value)
213
+ select = content_tag("select", opts, html_options)
214
+ if html_options["multiple"] && options.fetch(:include_hidden, true)
215
+ tag("input", :disabled => html_options["disabled"], :name => html_options["name"],
216
+ :type => "hidden", :value => "") + select
217
+ else
218
+ select
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+
225
+ class FormBuilder
226
+ # Generate select and country option tags with the provided name. A
227
+ # common use of this would be to allow users to select a country name inside a
228
+ # web form.
229
+ #
230
+ # See `FormOptionsHelper::country_select` for more information.
231
+ def country_select(method, priorities_or_options = {}, options_or_html_options = {}, html_options = {})
232
+ if priorities_or_options.is_a? Array
233
+ options = options_or_html_options
234
+ options[:priority] = priorities_or_options
235
+ else
236
+ options = priorities_or_options
237
+ html_options = options_or_html_options
238
+ end
239
+
240
+ @template.country_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
241
+ end
242
+
243
+ # Generate select and subregion option tags with the provided name. A
244
+ # common use of this would be to allow users to select a state subregion within
245
+ # a given country.
246
+ #
247
+ # See `FormOptionsHelper::subregion_select` for more information.
248
+ def subregion_select(method, parent_region_or_code, options = {}, html_options = {})
249
+ @template.subregion_select(@object_name, method, parent_region_or_code, objectify_options(options), @default_options.merge(html_options))
250
+ end
251
+ end
252
+
253
+ end
254
+ end
@@ -0,0 +1,5 @@
1
+ module Carmen
2
+ module Rails
3
+ VERSION = "1.0.1"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :carmen-rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carmen-country
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nilesh Patil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
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
+ - !ruby/object:Gem::Dependency
28
+ name: carmens
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Provides country_select and subregion_select form helpers.
56
+ email:
57
+ - nileshpatil47@yahoo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/carmen-rails.rb
66
+ - lib/carmen/rails/action_view/form_helper.rb
67
+ - lib/carmen/rails/version.rb
68
+ - lib/tasks/carmen-rails_tasks.rake
69
+ homepage: http://github.com/nileshpatil47/carmen-rails
70
+ licenses: []
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.2.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Rails adapter for Carmen
92
+ test_files: []