tastebook_country_select 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
1
+ module CountryCodes # :nodoc:
2
+ def self.method_missing(name, *args)
3
+ if match = /find_([^_]*)_by_([^_]*)/.match(name.to_s)
4
+ raise "1 argument expected, #{args.size} provided." unless args.size == 1
5
+
6
+ required = match[1]
7
+ request = match[2]
8
+ if valid_attributes.include?(request) && valid_attributes.include?(required)
9
+ @countries[request][args[0].to_s.downcase][required.to_sym] || nil rescue nil
10
+ else
11
+ raise "#{request} is not a valid attribute, valid attributes for find_*_by_* are: #{valid_attributes.join(', ')}."
12
+ end
13
+
14
+ elsif match = /find_by_(.*)/.match(name.to_s)
15
+ raise "1 argument expected, #{args.size} provided." unless args.size == 1
16
+
17
+ request = match[1]
18
+ if valid_attributes.include?(request)
19
+ @countries[request][args[0].to_s.downcase] || nil_countries_hash
20
+ else
21
+ raise "#{request} is not a valid attribute, valid attributes for find_by_* are: #{valid_attributes.join(', ')}."
22
+ end
23
+
24
+ else
25
+ raise NoMethodError.new("Method '#{name}' not supported")
26
+ end
27
+ end
28
+
29
+ def self.countries_for_select(*args)
30
+ # Ensure that each of the arguments is a valid attribute
31
+ args.each do |arg|
32
+ unless valid_attributes.include?(arg)
33
+ raise "#{arg} is not a valid attribute, valid attributes are: #{valid_attributes.join(', ')}"
34
+ end
35
+ end
36
+
37
+ # Build and return the desired array
38
+ @countries[@countries.keys.first].map do |index, country|
39
+ args.map { |a| country[a.to_sym] }
40
+ end
41
+ end
42
+
43
+ def self.valid_attributes
44
+ @countries.keys
45
+ end
46
+
47
+ def self.nil_countries_hash
48
+ hash = {}
49
+ valid_attributes.map { |attribute| hash[attribute.to_sym] = nil }
50
+ hash
51
+ end
52
+
53
+ def self.load_countries_from_yaml
54
+ # Load countries
55
+ countries_from_file = YAML::load(File.open(File.expand_path(File.join(File.dirname(__FILE__), 'countries.yml'))))
56
+
57
+ # Build indexes for each attribute
58
+ @countries = {}
59
+ countries_from_file.first.keys.each do |key|
60
+ @countries[key.to_s] = {}
61
+ countries_from_file.each { |country| @countries[key.to_s][country[key].to_s.downcase] = country }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module CountrySelect
2
+ VERSION = "2.0.2"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'country_select'
3
+
4
+ module ActionView
5
+ module Helpers
6
+ describe CountrySelect do
7
+ let!(:walrus) { Walrus.new }
8
+ let!(:template) { ActionView::Base.new }
9
+
10
+ let(:select_tag) do
11
+ "<select id=\"walrus_country_name\" name=\"walrus[country_name]\">"
12
+ end
13
+
14
+ let(:builder) { FormBuilder.new(:walrus, walrus, template, {}, Proc.new { }) }
15
+
16
+ describe "#country_select" do
17
+ let(:tag) { builder.country_select(:country_name) }
18
+
19
+ it "creates a select tag" do
20
+ tag.should include(select_tag)
21
+ end
22
+
23
+ it "creates option tags of the countries" do
24
+ COUNTRIES.each do |c|
25
+ c.gsub!(/'/,'&#x27;')
26
+ tag.should include("<option value=\"#{c}\">#{c}</option>")
27
+ end
28
+ end
29
+
30
+ it "selects the value of country_name" do
31
+ walrus.country_name = 'United States'
32
+ t = builder.country_select(:country_name)
33
+ t.should include("<option value=\"United States\" selected=\"selected\">United States</option>")
34
+ end
35
+ end
36
+
37
+ describe "#priority_countries" do
38
+ let(:tag) { builder.country_select(:country_name, ['United States']) }
39
+
40
+ it "puts the countries at the top" do
41
+ tag.should include("#{select_tag}<option value=\"United States")
42
+ end
43
+
44
+ it "inserts a divider" do
45
+ tag.should include(">United States</option><option value=\"\" disabled=\"disabled\">-------------</option>")
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tastebook_country_select
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 2
7
+ - 0
8
+ - 2
9
+ version: 2.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Stefan Penner
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-03-31 00:00:00 +05:30
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Provides a simple helper to get an HTML select list of countries. The list of countries comes from the ISO 3166 standard. While it is a relatively neutral source of country names, it will still offend some users.
22
+ email:
23
+ - stefan.penner@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - .rspec
33
+ - Gemfile
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - VERSION
38
+ - country_select.gemspec
39
+ - init.rb
40
+ - lib/country_select.rb
41
+ - lib/country_select/countries.yml
42
+ - lib/country_select/country_codes.rb
43
+ - lib/country_select/version.rb
44
+ - spec/country_select_spec.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: ""
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project: country_select
72
+ rubygems_version: 1.3.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Country Select Plugin
76
+ test_files:
77
+ - spec/country_select_spec.rb
78
+ - spec/spec_helper.rb