diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml new file mode 100644 index 0000000..bb29b65 --- /dev/null +++ b/.github/workflows/ruby.yml @@ -0,0 +1,81 @@ +name: Test +on: [push, pull_request] +jobs: + test: + strategy: + fail-fast: false + matrix: + gemfile: + - Gemfile + - gemfiles/Gemfile-rails-7-0 + - gemfiles/Gemfile-rails-6-1 + - gemfiles/Gemfile-rails-6-0 + - gemfiles/Gemfile-rails-5-2 + - gemfiles/Gemfile-rails-5-1 + - gemfiles/Gemfile-rails-5-0 + ruby: + - '3.3' + - '3.2' + - '3.1' + - '3.0' + - '2.7' + - '2.6' + - '2.5' + exclude: + - gemfile: Gemfile + ruby: '2.6' + - gemfile: Gemfile + ruby: '2.5' + - gemfile: gemfiles/Gemfile-rails-7-0 + ruby: '2.6' + - gemfile: gemfiles/Gemfile-rails-7-0 + ruby: '2.5' + - gemfile: gemfiles/Gemfile-rails-6-0 + ruby: '3.3' + - gemfile: gemfiles/Gemfile-rails-6-0 + ruby: '3.2' + - gemfile: gemfiles/Gemfile-rails-6-0 + ruby: '3.1' + - gemfile: gemfiles/Gemfile-rails-5-2 + ruby: '3.3' + - gemfile: gemfiles/Gemfile-rails-5-2 + ruby: '3.2' + - gemfile: gemfiles/Gemfile-rails-5-2 + ruby: '3.1' + - gemfile: gemfiles/Gemfile-rails-5-2 + ruby: '3.0' + - gemfile: gemfiles/Gemfile-rails-5-2 + ruby: '2.7' + - gemfile: gemfiles/Gemfile-rails-5-1 + ruby: '3.3' + - gemfile: gemfiles/Gemfile-rails-5-1 + ruby: '3.2' + - gemfile: gemfiles/Gemfile-rails-5-1 + ruby: '3.1' + - gemfile: gemfiles/Gemfile-rails-5-1 + ruby: '3.0' + - gemfile: gemfiles/Gemfile-rails-5-1 + ruby: '2.7' + - gemfile: gemfiles/Gemfile-rails-5-0 + ruby: '3.3' + - gemfile: gemfiles/Gemfile-rails-5-0 + ruby: '3.2' + - gemfile: gemfiles/Gemfile-rails-5-0 + ruby: '3.1' + - gemfile: gemfiles/Gemfile-rails-5-0 + ruby: '3.0' + - gemfile: gemfiles/Gemfile-rails-5-0 + ruby: '2.7' + runs-on: ubuntu-latest + env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps + BUNDLE_GEMFILE: ${{ matrix.gemfile }} + steps: + - uses: actions/checkout@v3 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true # runs bundle install and caches installed gems automatically + - name: Install + run: gem install bundler -v 2.3.26 + - name: Run tests + run: bundle exec rake diff --git a/.gitignore b/.gitignore index cd52aca..83106fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .bundle/ +.idea/ log/*.log pkg/ Gemfile.lock diff --git a/.travis.yml b/.travis.yml index 37e7e91..5183ddb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,19 @@ +language: ruby rvm: - - 1.9.2 - - 1.9.3 - - 2.0.0 -env: - - "RAILS_VERSION=3.1.0" - - "RAILS_VERSION=3.2.0" - - "RAILS_VERSION=4.0.0" + - 2.3.8 + - 2.4.9 + - 2.5 + - 2.6 +gemfile: + - gemfiles/rails_4.1.gemfile + - gemfiles/rails_4.2.gemfile + - gemfiles/rails_5.0.gemfile + - gemfiles/rails_5.1.gemfile + - gemfiles/rails_5.2.gemfile + - gemfiles/rails_6.0.gemfile matrix: exclude: - - rvm: 1.9.2 - env: "RAILS_VERSION=4.0.0" -branches: - only: - - master + - rvm: 2.3.8 + gemfile: gemfiles/rails_6.0.gemfile + - rvm: 2.4.9 + gemfile: gemfiles/rails_6.0.gemfile diff --git a/Appraisals b/Appraisals deleted file mode 100644 index 635f8c4..0000000 --- a/Appraisals +++ /dev/null @@ -1,7 +0,0 @@ -appraise "rails-3" do - gem "rails", "~> 3.2" -end - -appraise "rails-4" do - gem "rails", "~> 4.0" -end diff --git a/Gemfile b/Gemfile index 5f6e3ae..c52c172 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,8 @@ -source "http://rubygems.org" +source 'https://rubygems.org' gemspec -gem 'appraisal', "1.0.0.beta3" -gem "debugger" +gem 'pry' +gem 'rails', '~> 7.1.0' +gem 'sprockets', '~> 4.0' +gem 'test-unit', '~> 3.0' diff --git a/README.md b/README.md index 541389b..8448b86 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,12 @@ -# NOT ACTIVELY MAINTAINED - -I haven't time in over a year to properly support this project. - # carmen-rails -carmen-rails is a Rails 3 plugin that supplies two new form helper methods: +carmen-rails is a Rails 5-7 plugin that supplies two new form helper methods: `country_select` and `subregion_select`. It uses [carmen](http://github.com/jim/carmen) as its source of geographic data. ## Requirements -carmen-rails requires Ruby 1.9.2 or greater. +carmen-rails requires Ruby 2.7 or greater (although might work with older versions) ## Installation diff --git a/Rakefile b/Rakefile index 33e754c..5453082 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,4 @@ -require "rubygems" +require 'rubygems' require 'bundler/setup' require 'rake' @@ -15,4 +15,4 @@ Rake::TestTask.new(:test) do |t| end desc 'Default: run unit tests.' -task :default => :test +task default: :test diff --git a/carmen-rails.gemspec b/carmen-rails.gemspec index 6113faa..f3ae72d 100644 --- a/carmen-rails.gemspec +++ b/carmen-rails.gemspec @@ -1,21 +1,22 @@ -$:.push File.expand_path("../lib", __FILE__) +$:.push File.expand_path('../lib', __FILE__) -require "carmen/rails/version" +require 'carmen/rails/region_select_tag' +require 'carmen/rails/version' Gem::Specification.new do |s| - s.name = "carmen-rails" + s.name = 'carmen-rails' s.version = Carmen::Rails::VERSION - s.authors = ["Jim Benton"] - s.email = ["jim@autonomousmachine.com"] - s.homepage = "http://github.com/jim/carmen-rails" - s.summary = "Rails adapter for Carmen" - s.description = "Provides country_select and subregion_select form helpers." + s.authors = ['Jim Benton'] + s.email = ['jim@autonomousmachine.com'] + s.homepage = 'http://github.com/jim/carmen-rails' + s.summary = 'Rails adapter for Carmen' + s.description = 'Provides country_select and subregion_select form helpers.' - s.files = Dir["{lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"] - s.test_files = Dir["spec/**/*"] + s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile', 'README.md'] + s.test_files = Dir['spec/**/*'] - s.add_dependency "rails" - s.add_dependency "carmen", "~> 1.0.0" + s.add_dependency 'rails' + s.add_dependency 'carmen', '~> 1.0' - s.add_development_dependency "minitest" + s.add_development_dependency 'minitest' end diff --git a/gemfiles/Gemfile-rails-5-0 b/gemfiles/Gemfile-rails-5-0 new file mode 100644 index 0000000..8b5803d --- /dev/null +++ b/gemfiles/Gemfile-rails-5-0 @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec path: '..' + +gem 'rails', '~> 5.0.0' +gem 'sprockets', '~> 3.7' +gem 'test-unit', '~> 3.0' diff --git a/gemfiles/Gemfile-rails-5-1 b/gemfiles/Gemfile-rails-5-1 new file mode 100644 index 0000000..95559f4 --- /dev/null +++ b/gemfiles/Gemfile-rails-5-1 @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec path: '..' + +gem 'rails', '~> 5.1.0' +gem 'sprockets', '~> 3.7' +gem 'test-unit', '~> 3.0' diff --git a/gemfiles/Gemfile-rails-5-2 b/gemfiles/Gemfile-rails-5-2 new file mode 100644 index 0000000..86551a1 --- /dev/null +++ b/gemfiles/Gemfile-rails-5-2 @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec path: '..' + +gem 'rails', '~> 5.2.0' +gem 'sprockets', '~> 3.7' +gem 'test-unit', '~> 3.0' diff --git a/gemfiles/Gemfile-rails-6-0 b/gemfiles/Gemfile-rails-6-0 new file mode 100644 index 0000000..a85acdc --- /dev/null +++ b/gemfiles/Gemfile-rails-6-0 @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec path: '..' + +gem 'rails', '~> 6.0.0' +gem 'sprockets', '~> 4.0' +gem 'test-unit', '~> 3.0' diff --git a/gemfiles/Gemfile-rails-6-1 b/gemfiles/Gemfile-rails-6-1 new file mode 100644 index 0000000..c445d70 --- /dev/null +++ b/gemfiles/Gemfile-rails-6-1 @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec path: '..' + +gem 'rails', '~> 6.1.0' +gem 'sprockets', '~> 4.0' +gem 'test-unit', '~> 3.0' diff --git a/gemfiles/Gemfile-rails-7-0 b/gemfiles/Gemfile-rails-7-0 new file mode 100644 index 0000000..6f230ee --- /dev/null +++ b/gemfiles/Gemfile-rails-7-0 @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gemspec path: '..' + +gem 'rails', '~> 7.0.0' +gem 'sprockets', '~> 4.0' +gem 'test-unit', '~> 3.0' diff --git a/gemfiles/rails_3.gemfile b/gemfiles/rails_3.gemfile deleted file mode 100644 index 6bfabe4..0000000 --- a/gemfiles/rails_3.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -# This file was generated by Appraisal - -source "http://rubygems.org" - -gem "appraisal", "1.0.0.beta3" -gem "debugger" -gem "rails", "~> 3.2" - -gemspec :path=>".././" diff --git a/gemfiles/rails_3.gemfile.lock b/gemfiles/rails_3.gemfile.lock deleted file mode 100644 index 38b8af7..0000000 --- a/gemfiles/rails_3.gemfile.lock +++ /dev/null @@ -1,111 +0,0 @@ -PATH - remote: .././ - specs: - carmen-rails (1.0.0) - carmen (~> 1.0.0) - rails - -GEM - remote: http://rubygems.org/ - specs: - actionmailer (3.2.17) - actionpack (= 3.2.17) - mail (~> 2.5.4) - actionpack (3.2.17) - activemodel (= 3.2.17) - activesupport (= 3.2.17) - builder (~> 3.0.0) - erubis (~> 2.7.0) - journey (~> 1.0.4) - rack (~> 1.4.5) - rack-cache (~> 1.2) - rack-test (~> 0.6.1) - sprockets (~> 2.2.1) - activemodel (3.2.17) - activesupport (= 3.2.17) - builder (~> 3.0.0) - activerecord (3.2.17) - activemodel (= 3.2.17) - activesupport (= 3.2.17) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activeresource (3.2.17) - activemodel (= 3.2.17) - activesupport (= 3.2.17) - activesupport (3.2.17) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) - appraisal (1.0.0.beta3) - bundler - rake - thor (>= 0.14.0) - arel (3.0.3) - builder (3.0.4) - carmen (1.0.1) - unicode_utils (~> 1.4.0) - columnize (0.3.6) - debugger (1.6.6) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.2) - debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.2) - erubis (2.7.0) - hike (1.2.3) - i18n (0.6.9) - journey (1.0.4) - json (1.8.1) - mail (2.5.4) - mime-types (~> 1.16) - treetop (~> 1.4.8) - mime-types (1.25.1) - minitest (5.3.0) - multi_json (1.9.0) - polyglot (0.3.4) - rack (1.4.5) - rack-cache (1.2) - rack (>= 0.4) - rack-ssl (1.3.3) - rack - rack-test (0.6.2) - rack (>= 1.0) - rails (3.2.17) - actionmailer (= 3.2.17) - actionpack (= 3.2.17) - activerecord (= 3.2.17) - activeresource (= 3.2.17) - activesupport (= 3.2.17) - bundler (~> 1.0) - railties (= 3.2.17) - railties (3.2.17) - actionpack (= 3.2.17) - activesupport (= 3.2.17) - rack-ssl (~> 1.3.2) - rake (>= 0.8.7) - rdoc (~> 3.4) - thor (>= 0.14.6, < 2.0) - rake (10.1.1) - rdoc (3.12.2) - json (~> 1.4) - sprockets (2.2.2) - hike (~> 1.2) - multi_json (~> 1.0) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - thor (0.18.1) - tilt (1.4.1) - treetop (1.4.15) - polyglot - polyglot (>= 0.3.1) - tzinfo (0.3.38) - unicode_utils (1.4.0) - -PLATFORMS - ruby - -DEPENDENCIES - appraisal (= 1.0.0.beta3) - carmen-rails! - debugger - minitest - rails (~> 3.2) diff --git a/gemfiles/rails_4.gemfile b/gemfiles/rails_4.gemfile deleted file mode 100644 index e76d38f..0000000 --- a/gemfiles/rails_4.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -# This file was generated by Appraisal - -source "http://rubygems.org" - -gem "appraisal", "1.0.0.beta3" -gem "debugger" -gem "rails", "~> 4.0" - -gemspec :path=>".././" diff --git a/gemfiles/rails_4.gemfile.lock b/gemfiles/rails_4.gemfile.lock deleted file mode 100644 index f9c5048..0000000 --- a/gemfiles/rails_4.gemfile.lock +++ /dev/null @@ -1,105 +0,0 @@ -PATH - remote: .././ - specs: - carmen-rails (1.0.0) - carmen (~> 1.0.0) - rails - -GEM - remote: http://rubygems.org/ - specs: - actionmailer (4.0.3) - actionpack (= 4.0.3) - mail (~> 2.5.4) - actionpack (4.0.3) - activesupport (= 4.0.3) - builder (~> 3.1.0) - erubis (~> 2.7.0) - rack (~> 1.5.2) - rack-test (~> 0.6.2) - activemodel (4.0.3) - activesupport (= 4.0.3) - builder (~> 3.1.0) - activerecord (4.0.3) - activemodel (= 4.0.3) - activerecord-deprecated_finders (~> 1.0.2) - activesupport (= 4.0.3) - arel (~> 4.0.0) - activerecord-deprecated_finders (1.0.3) - activesupport (4.0.3) - i18n (~> 0.6, >= 0.6.4) - minitest (~> 4.2) - multi_json (~> 1.3) - thread_safe (~> 0.1) - tzinfo (~> 0.3.37) - appraisal (1.0.0.beta3) - bundler - rake - thor (>= 0.14.0) - arel (4.0.2) - atomic (1.1.15) - builder (3.1.4) - carmen (1.0.1) - unicode_utils (~> 1.4.0) - columnize (0.3.6) - debugger (1.6.6) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.2) - debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.2) - erubis (2.7.0) - hike (1.2.3) - i18n (0.6.9) - mail (2.5.4) - mime-types (~> 1.16) - treetop (~> 1.4.8) - mime-types (1.25.1) - minitest (4.7.5) - multi_json (1.9.0) - polyglot (0.3.4) - rack (1.5.2) - rack-test (0.6.2) - rack (>= 1.0) - rails (4.0.3) - actionmailer (= 4.0.3) - actionpack (= 4.0.3) - activerecord (= 4.0.3) - activesupport (= 4.0.3) - bundler (>= 1.3.0, < 2.0) - railties (= 4.0.3) - sprockets-rails (~> 2.0.0) - railties (4.0.3) - actionpack (= 4.0.3) - activesupport (= 4.0.3) - rake (>= 0.8.7) - thor (>= 0.18.1, < 2.0) - rake (10.1.1) - sprockets (2.11.0) - hike (~> 1.2) - multi_json (~> 1.0) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - sprockets-rails (2.0.1) - actionpack (>= 3.0) - activesupport (>= 3.0) - sprockets (~> 2.8) - thor (0.18.1) - thread_safe (0.2.0) - atomic (>= 1.1.7, < 2) - tilt (1.4.1) - treetop (1.4.15) - polyglot - polyglot (>= 0.3.1) - tzinfo (0.3.38) - unicode_utils (1.4.0) - -PLATFORMS - ruby - -DEPENDENCIES - appraisal (= 1.0.0.beta3) - carmen-rails! - debugger - minitest - rails (~> 4.0) diff --git a/lib/carmen-rails.rb b/lib/carmen-rails.rb index 5ed3398..f598ba7 100644 --- a/lib/carmen-rails.rb +++ b/lib/carmen-rails.rb @@ -1,4 +1,5 @@ require 'carmen' +require 'carmen/rails/region_select_tag' require 'carmen/rails/action_view/form_helper' require 'carmen/rails/version' diff --git a/lib/carmen/rails/action_view/form_helper.rb b/lib/carmen/rails/action_view/form_helper.rb index 6247c81..e33e9b1 100644 --- a/lib/carmen/rails/action_view/form_helper.rb +++ b/lib/carmen/rails/action_view/form_helper.rb @@ -1,7 +1,6 @@ module ActionView module Helpers module FormOptionsHelper - # Generate select and subregion option tags for the given object and method. A # common use of this would be to allow users to select a state subregion within # a given country. @@ -22,7 +21,7 @@ module FormOptionsHelper # subregion_select(@object, :region, {priority: ['US', 'CA']}, class: 'region') # # Returns an `html_safe` string containing the HTML for a select element. - def subregion_select(object, method, parent_region_or_code, options={}, html_options={}) + def subregion_select(object, method, parent_region_or_code, options = {}, html_options = {}) parent_region = determine_parent(parent_region_or_code) tag = instance_tag(object, method, self, options) tag.to_region_select_tag(parent_region, options, html_options) @@ -77,10 +76,10 @@ def country_select(object, method, priorities_or_options = {}, options_or_html_o # region_options_for_select(@region.subregions, 'US', priority: ['US', 'CA']) # # Returns an `html_safe` string containing option tags. - def region_options_for_select(regions, selected=nil, options={}) + def region_options_for_select(regions, selected = nil, options = {}) options.stringify_keys! priority_region_codes = options['priority'] || [] - region_options = "" + region_options = '' unless priority_region_codes.empty? unless regions.respond_to?(:coded) @@ -93,7 +92,7 @@ def region_options_for_select(regions, selected=nil, options={}) end.compact unless priority_regions.empty? region_options += options_for_select(priority_regions, selected) - region_options += "" + region_options += '' # If a priority region is selected, don't select it again in the main list. # This prevents some browsers from selecting the second occurance of this region, @@ -103,7 +102,7 @@ def region_options_for_select(regions, selected=nil, options={}) end main_options = regions.map { |r| [r.name, r.code] } - main_options.sort!{|a, b| a.first.to_s <=> b.first.to_s} + main_options.sort! { |a, b| a.first.to_s <=> b.first.to_s } main_options.unshift [options['prompt'], ''] if options['prompt'] region_options += options_for_select(main_options, selected) @@ -127,7 +126,7 @@ def region_options_for_select(regions, selected=nil, options={}) # country_select_tag('country_code', {priority: ['US', 'CA']}, class: 'region') # # Returns an `html_safe` string containing the HTML for a select element. - def country_select_tag(name, value, options={}) + def country_select_tag(name, value, options = {}) subregion_select_tag(name, value, Carmen::World.instance, options) end @@ -154,22 +153,24 @@ def subregion_select_tag(name, value, parent_region_or_code, options = {}, html_ options.stringify_keys! parent_region = determine_parent(parent_region_or_code) opts = region_options_for_select(parent_region.subregions, value, options) - html_options = {"name" => name, - "id" => sanitize_to_id(name)}.update(html_options.stringify_keys) + html_options = { 'name' => name, 'id' => sanitize_to_id(name) }.update(html_options.stringify_keys) content_tag(:select, opts, html_options) end private def instance_tag(object_name, method_name, template_object, options = {}) - if Rails::VERSION::MAJOR == 3 + if ::Rails::VERSION::MAJOR == 3 InstanceTag.new(object_name, method_name, template_object, options.delete(:object)) else - ActionView::Helpers::Tags::Base.new(object_name, method_name, template_object, options || {}) + if [4, 5].include? ::Rails::VERSION::MAJOR + ActionView::Helpers::Tags::Base.new(object_name, method_name, template_object, options || {}) + else + ActionView::Helpers::Tags::Select.new(object_name, method_name, template_object, nil, options || {}, nil) + end end end - def determine_parent(parent_region_or_code) case parent_region_or_code when String @@ -184,45 +185,30 @@ def determine_parent(parent_region_or_code) end end - if Rails::VERSION::MAJOR == 3 + if ::Rails::VERSION::MAJOR == 3 class InstanceTag def to_region_select_tag(parent_region, options = {}, html_options = {}) html_options = html_options.stringify_keys add_default_name_and_id(html_options) priority_regions = options[:priority] || [] value = options[:selected] ? options[:selected] : value(object) - opts = add_options(region_options_for_select(parent_region.subregions, value, :priority => priority_regions), options, value) - content_tag("select", opts, html_options) + opts = add_options( + region_options_for_select(parent_region.subregions, value, priority: priority_regions), options, value) + content_tag('select', opts, html_options) end end end - if [4, 5].include? Rails::VERSION::MAJOR + if [4, 5].include? ::Rails::VERSION::MAJOR module Tags class Base - def to_region_select_tag(parent_region, options = {}, html_options = {}) - html_options = html_options.stringify_keys - add_default_name_and_id(html_options) - - if (Rails::VERSION::MAJOR == 4 && !select_not_required?(html_options)) || - (Rails::VERSION::MAJOR == 5 && placeholder_required?(html_options)) - raise ArgumentError, "include_blank cannot be false for a required field." if options[:include_blank] == false - options[:include_blank] ||= true unless options[:prompt] - end - - value = options[:selected] ? options[:selected] : value(object) - priority_regions = options[:priority] || [] - opts = add_options(region_options_for_select(parent_region.subregions, value, - :priority => priority_regions), - options, value) - select = content_tag("select", opts, html_options) - if html_options["multiple"] && options.fetch(:include_hidden, true) - tag("input", :disabled => html_options["disabled"], :name => html_options["name"], - :type => "hidden", :value => "") + select - else - select - end - end + include Carmen::Rails::RegionSelectTag + end + end + else + module Tags + class Select + include Carmen::Rails::RegionSelectTag end end end @@ -251,9 +237,9 @@ def country_select(method, priorities_or_options = {}, options_or_html_options = # # See `FormOptionsHelper::subregion_select` for more information. def subregion_select(method, parent_region_or_code, options = {}, html_options = {}) - @template.subregion_select(@object_name, method, parent_region_or_code, objectify_options(options), @default_options.merge(html_options)) + @template.subregion_select(@object_name, method, parent_region_or_code, objectify_options(options), + @default_options.merge(html_options)) end end - end end diff --git a/lib/carmen/rails/region_select_tag.rb b/lib/carmen/rails/region_select_tag.rb new file mode 100644 index 0000000..9cfebcb --- /dev/null +++ b/lib/carmen/rails/region_select_tag.rb @@ -0,0 +1,29 @@ +module Carmen + module Rails + module RegionSelectTag + def to_region_select_tag(parent_region, options = {}, html_options = {}) + html_options = html_options.stringify_keys + add_default_name_and_id(html_options) + if (::Rails::VERSION::MAJOR == 4 && !select_not_required?(html_options)) || + ([5, 6, 7].include?(::Rails::VERSION::MAJOR) && placeholder_required?(html_options)) + raise ArgumentError, 'include_blank cannot be false for a required field.' if options[:include_blank] == false + + options[:include_blank] ||= true unless options[:prompt] + end + + value = options[:selected] ? options[:selected] : (method(:value).arity.zero? ? value() : value(object)) + priority_regions = options[:priority] || [] + opts = add_options(region_options_for_select(parent_region.subregions, value, + priority: priority_regions), + options, value) + select = content_tag('select', opts, html_options) + if html_options['multiple'] && options.fetch(:include_hidden, true) + tag('input', disabled: html_options['disabled'], name: html_options['name'], + type: 'hidden', value: '') + select + else + select + end + end + end + end +end diff --git a/lib/carmen/rails/version.rb b/lib/carmen/rails/version.rb index 3efeb4c..860771e 100644 --- a/lib/carmen/rails/version.rb +++ b/lib/carmen/rails/version.rb @@ -1,5 +1,5 @@ module Carmen module Rails - VERSION = "1.0.1" + VERSION = '1.0.1' end end diff --git a/lib/tasks/carmen-rails_tasks.rake b/lib/tasks/carmen-rails_tasks.rake deleted file mode 100644 index 98a799d..0000000 --- a/lib/tasks/carmen-rails_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :carmen-rails do -# # Task goes here -# end diff --git a/test/carmen/action_view/helpers/form_helper_test.rb b/test/carmen/action_view/helpers/carmen_view_helper_test.rb similarity index 86% rename from test/carmen/action_view/helpers/form_helper_test.rb rename to test/carmen/action_view/helpers/carmen_view_helper_test.rb index 5b5fd29..755d9f9 100644 --- a/test/carmen/action_view/helpers/form_helper_test.rb +++ b/test/carmen/action_view/helpers/carmen_view_helper_test.rb @@ -1,12 +1,18 @@ require 'test_helper' -class CarmenViewHelperTest < MiniTest::Unit::TestCase +class CarmenViewHelperTest < Minitest::Test include ActionView::Helpers::FormOptionsHelper include ActionView::Helpers::FormTagHelper - include ActionDispatch::Assertions::SelectorAssertions + + if ::Rails.const_defined?(:Dom) + include ::Rails::Dom::Testing::Assertions::SelectorAssertions + else + include ActionDispatch::Assertions::SelectorAssertions + end def setup @object = OpenStruct.new + def @object.to_s; 'object'; end end @@ -27,15 +33,19 @@ def test_basic_country_select assert_equal_markup(expected, html) end + def document_root_element + Nokogiri.parse(@html) + end + def test_country_selected_value - @html = country_select(:object, :country_code, :selected => 'OC') + @html = country_select(:object, :country_code, selected: 'OC') assert_select('option[selected="selected"][value="OC"]') end def test_country_selected_object_option @object.country_code = 'OC' - override_object = OpenStruct.new(:country_code => 'ES') - @html = country_select(@object, :country_code, {:object => override_object}) + override_object = OpenStruct.new(country_code: 'ES') + @html = country_select(@object, :country_code, { object: override_object }) assert_select('option[selected="selected"][value="ES"]') end @@ -54,7 +64,7 @@ def test_basic_country_select_tag end def test_country_select_tag_with_prompt - html = country_select_tag('attribute_name', nil, :prompt => 'Please Select') + html = country_select_tag('attribute_name', nil, prompt: 'Please Select') expected = <<-HTML @@ -104,7 +115,7 @@ def test_priority_country_select_deprecated_api def test_country_select_selected_priority_region_not_selected_twice @object.country_code = 'ES' - html = country_select(@object, :country_code, :priority => ['ES']) + html = country_select(@object, :country_code, priority: ['ES']) expected = <<-HTML + HTML @@ -135,6 +147,7 @@ def test_subregion_select_using_parent_code expected = <<-HTML HTML @@ -158,7 +171,7 @@ def test_subregion_select_using_parent_code_array def test_subregion_selected_value oceania = Carmen::Country.coded('OC') - @html = subregion_select(:object, :subregion_code, oceania, :selected => 'AO') + @html = subregion_select(:object, :subregion_code, oceania, selected: 'AO') assert_select('option[selected="selected"][value="AO"]') end @@ -171,7 +184,8 @@ def test_subregion_selected_value_with_priority_and_selected_options def test_html_options_for_selected_value_with_priority_and_selected_options oceania = Carmen::Country.coded('OC') - @html = subregion_select(:object, :subregion_code, oceania, { priority: ['AO'], selected: 'AO' }, { class: :test_html_options}) + @html = subregion_select(:object, :subregion_code, oceania, { priority: ['AO'], selected: 'AO' }, + { class: :test_html_options }) assert_select('.test_html_options') end @@ -181,6 +195,7 @@ def test_basic_subregion_select_tag expected = <<-HTML HTML @@ -196,10 +211,11 @@ def test_subregion_select_tag_with_priority + HTML - html = subregion_select_tag(:subregion_code, nil, oceania, :priority => ['AO']) + html = subregion_select_tag(:subregion_code, nil, oceania, priority: ['AO']) assert_equal_markup(expected, html) end @@ -210,10 +226,11 @@ def test_subregion_select_tag_with_prompt HTML - html = subregion_select_tag(:subregion_code, nil, oceania, :prompt => 'Please select') + html = subregion_select_tag(:subregion_code, nil, oceania, prompt: 'Please select') assert_equal_markup(expected, html) end @@ -246,13 +263,13 @@ def test_region_options_for_select_with_array_of_regions_and_priority HTML - html = region_options_for_select(regions, nil, :priority => ['ES']) + html = region_options_for_select(regions, nil, priority: ['ES']) assert_equal_markup(expected, html) end def test_form_builder_country_select - form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{}) + form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}) html = form.country_select('attribute_name') expected = <<-HTML @@ -268,14 +285,14 @@ def test_form_builder_country_select def test_form_builder_selected_country @object.country_code = 'OC' - form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{}) + form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}) @html = form.country_select('country_code') assert_select('option[selected="selected"][value="OC"]') end def test_form_builder_country_select_deprecated_api - form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{}) + form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}) html = form.country_select('attribute_name', ['ES']) expected = <<-HTML @@ -292,11 +309,12 @@ def test_form_builder_country_select_deprecated_api end def test_form_builder_subregion_select - form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{}) + form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}) html = form.subregion_select(:subregion_code, 'OC') expected = <<-HTML HTML @@ -305,7 +323,7 @@ def test_form_builder_subregion_select def test_form_builder_selected_subregion @object.subregion_code = 'AO' - form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}, lambda{}) + form = ActionView::Helpers::FormBuilder.new(:object, @object, self, {}) @html = form.subregion_select(:subregion_code, 'OC') assert_select('option[selected="selected"][value="AO"]') diff --git a/test/spec_data/data/world.yml b/test/spec_data/data/world.yml new file mode 100644 index 0000000..99d6382 --- /dev/null +++ b/test/spec_data/data/world.yml @@ -0,0 +1,13 @@ +--- +- alpha_2_code: OC + alpha_3_code: OCE + numeric_code: "001" + type: country +- alpha_2_code: EU + alpha_3_code: EUR + numeric_code: "002" + type: country +- alpha_2_code: ES + alpha_3_code: EST + numeric_code: "003" + type: country diff --git a/test/spec_data/data/world/oc.yml b/test/spec_data/data/world/oc.yml new file mode 100644 index 0000000..a4d2144 --- /dev/null +++ b/test/spec_data/data/world/oc.yml @@ -0,0 +1,5 @@ +--- +- code: AO + type: province +- code: AT + type: province diff --git a/test/spec_data/data/world/oc/ao.yml b/test/spec_data/data/world/oc/ao.yml new file mode 100644 index 0000000..1945668 --- /dev/null +++ b/test/spec_data/data/world/oc/ao.yml @@ -0,0 +1,3 @@ +--- +- code: LO + type: city diff --git a/test/spec_data/locale/de/world.yml b/test/spec_data/locale/de/world.yml new file mode 100644 index 0000000..a8fdf1f --- /dev/null +++ b/test/spec_data/locale/de/world.yml @@ -0,0 +1,7 @@ +--- +de: + world: + eu: + common_name: Eurasia + name: Das großartige Staat von Eurasia + official_name: Das großartige Staat von Eurasia diff --git a/test/spec_data/locale/en/world.yml b/test/spec_data/locale/en/world.yml new file mode 100644 index 0000000..b47e88c --- /dev/null +++ b/test/spec_data/locale/en/world.yml @@ -0,0 +1,15 @@ +--- +en: + world: + oc: + common_name: Oceania + name: Oceania + official_name: The Superstate of Oceania + eu: + common_name: Eurasia + name: Eurasia + official_name: The Superstate of Eurasia + es: + common_name: Eastasia + name: Eastasia + official_name: The Superstate of Eastasia diff --git a/test/spec_data/locale/en/world/oc.yml b/test/spec_data/locale/en/world/oc.yml new file mode 100644 index 0000000..b007c6c --- /dev/null +++ b/test/spec_data/locale/en/world/oc.yml @@ -0,0 +1,8 @@ +--- +en: + world: + oc: + ao: + name: Airstrip One + at: + name: Airstrip-Two diff --git a/test/spec_data/locale/en/world/oc/ao.yml b/test/spec_data/locale/en/world/oc/ao.yml new file mode 100644 index 0000000..38b8951 --- /dev/null +++ b/test/spec_data/locale/en/world/oc/ao.yml @@ -0,0 +1,7 @@ +--- +en: + world: + oc: + ao: + lo: + name: London diff --git a/test/spec_data/overlay/data/world.yml b/test/spec_data/overlay/data/world.yml new file mode 100644 index 0000000..2756cf4 --- /dev/null +++ b/test/spec_data/overlay/data/world.yml @@ -0,0 +1,12 @@ +--- +- alpha_2_code: SE + alpha_3_code: SEA + numeric_code: "004" + common_name: Sealand + name: Sealand + official_name: The Principality of Sealand + type: fort +- alpha_2_code: EU + _enabled: false +- alpha_2_code: ES + official_name: The Wonderous Country of Eastasia diff --git a/test/spec_data/overlay/locale/de/world.yml b/test/spec_data/overlay/locale/de/world.yml new file mode 100644 index 0000000..d5fd86b --- /dev/null +++ b/test/spec_data/overlay/locale/de/world.yml @@ -0,0 +1,3 @@ +--- +de: + world: diff --git a/test/spec_data/overlay/locale/en/world.yml b/test/spec_data/overlay/locale/en/world.yml new file mode 100644 index 0000000..1dd67be --- /dev/null +++ b/test/spec_data/overlay/locale/en/world.yml @@ -0,0 +1,10 @@ +--- +en: + world: + es: + official_name: The Wonderous Country of Eastasia + se: + common_name: Sealand + name: Sealand + official_name: The Principality of Sealand + diff --git a/test/spec_data/overlay/locale/en/world/oc.yml b/test/spec_data/overlay/locale/en/world/oc.yml new file mode 100644 index 0000000..a8ceb29 --- /dev/null +++ b/test/spec_data/overlay/locale/en/world/oc.yml @@ -0,0 +1,6 @@ +--- +en: + world: + oc: + ao: + name: Airstrip Uno diff --git a/test/spec_data/overlay/locale/zz/world.yml b/test/spec_data/overlay/locale/zz/world.yml new file mode 100644 index 0000000..0e92eef --- /dev/null +++ b/test/spec_data/overlay/locale/zz/world.yml @@ -0,0 +1,5 @@ +--- +zz: + world: + es: + official_name: The Zonderous Zountry of Zeastasia diff --git a/test/spec_data/overlay/locale/zz/world/oc.yml b/test/spec_data/overlay/locale/zz/world/oc.yml new file mode 100644 index 0000000..aaf3b46 --- /dev/null +++ b/test/spec_data/overlay/locale/zz/world/oc.yml @@ -0,0 +1,6 @@ +--- +zz: + world: + oc: + ao: + name: Zairstrip Zuno diff --git a/test/test_helper.rb b/test/test_helper.rb index bd63c69..1a6835d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,21 +9,20 @@ require 'rails' require 'carmen-rails' require 'ostruct' -require 'debugger' -MiniTest::Spec.register_spec_type(/.*/, ActionView::TestCase) +Minitest::Spec.register_spec_type(/.*/, ActionView::TestCase) Carmen.clear_data_paths -carmen_path = File.expand_path('../../../carmen', __FILE__) +carmen_path = File.expand_path('..', __FILE__) Carmen.append_data_path(carmen_path + '/spec_data/data') locale_path = carmen_path + '/spec_data/locale' Carmen.i18n_backend = Carmen::I18n::Simple.new(locale_path) -class MiniTest::Unit::TestCase - def assert_equal_markup(expected, actual, message=nil) +class Minitest::Test + def assert_equal_markup(expected, actual, message = nil) assert_equal(clean_markup(expected), clean_markup(actual), message) end