From 56162eebd39d2f556c39129624d98dfe2a701715 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 29 Apr 2015 16:42:42 +0200 Subject: [PATCH 1/6] ensure to return symbols from SE::Enum --- lib/simple_enum/attribute.rb | 2 +- lib/simple_enum/enum.rb | 14 ++++++++++---- lib/simple_enum/view_helpers.rb | 1 + simple_enum.gemspec | 2 +- spec/simple_enum/attribute_spec.rb | 16 ++++++++-------- spec/simple_enum/enum_spec.rb | 20 +++++++------------- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lib/simple_enum/attribute.rb b/lib/simple_enum/attribute.rb index 0da142a..079c3eb 100644 --- a/lib/simple_enum/attribute.rb +++ b/lib/simple_enum/attribute.rb @@ -77,7 +77,7 @@ def generate_enum_scope_methods_for(enum, accessor) return unless respond_to?(:scope) enum.each_pair do |key, value| - scope "#{accessor.prefix}#{key.pluralize}", -> { accessor.scope(self, value) } + scope "#{accessor.prefix}#{key.to_s.pluralize}", -> { accessor.scope(self, value) } end end end diff --git a/lib/simple_enum/enum.rb b/lib/simple_enum/enum.rb index 9e8f431..da19b36 100644 --- a/lib/simple_enum/enum.rb +++ b/lib/simple_enum/enum.rb @@ -1,12 +1,14 @@ require 'active_support/core_ext/string' +require 'active_support/core_ext/hash' module SimpleEnum class Enum - attr_reader :name, :hash + attr_reader :name def initialize(name, hash) @name = name.to_s @hash = hash + @symbols_hash = hash.symbolize_keys end def include?(key) @@ -26,16 +28,16 @@ def value(key) alias_method :[], :value def each_pair(&block) - hash.each_pair(&block) + symbols_hash.each_pair(&block) end alias_method :each, :each_pair def map(&block) - hash.map(&block) + symbols_hash.map(&block) end def keys - hash.keys + symbols_hash.keys end def values_at(*keys) @@ -46,5 +48,9 @@ def values_at(*keys) def to_s name end + + # Private access to hash and symbolized hash + private + attr_reader :hash, :symbols_hash end end diff --git a/lib/simple_enum/view_helpers.rb b/lib/simple_enum/view_helpers.rb index 136bd72..1836b81 100644 --- a/lib/simple_enum/view_helpers.rb +++ b/lib/simple_enum/view_helpers.rb @@ -23,6 +23,7 @@ def enum_option_pairs(record, enum, encode_as_value = false) record = record.class unless record.respond_to?(reader) record.send(reader).map { |key, value| + key = key.to_s name = record.human_enum_name(enum, key) if record.respond_to?(:human_enum_name) name ||= translate_enum_key(enum, key) [name, encode_as_value ? value : key] diff --git a/simple_enum.gemspec b/simple_enum.gemspec index 0430aa2..ea1011e 100644 --- a/simple_enum.gemspec +++ b/simple_enum.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |s| s.summary = "Simple enum-like field support for models." s.description = "Provides enum-like fields for ActiveRecord, ActiveModel and Mongoid models." - s.required_ruby_version = ">= 1.9.3" + s.required_ruby_version = ">= 2.0.0" s.required_rubygems_version = ">= 2.0.0" s.authors = ["Lukas Westermann"] diff --git a/spec/simple_enum/attribute_spec.rb b/spec/simple_enum/attribute_spec.rb index 2483bf1..107293f 100644 --- a/spec/simple_enum/attribute_spec.rb +++ b/spec/simple_enum/attribute_spec.rb @@ -134,22 +134,22 @@ def generate_enum_spec_extension_for(enum, accessor) end it 'delegates #male? to accessor' do - expect(accessor).to receive(:selected?).with(subject, 'male') { true } + expect(accessor).to receive(:selected?).with(subject, :male) { true } expect(subject.male?).to be_truthy end it 'delegates #female? to accessor' do - expect(accessor).to receive(:selected?).with(subject, 'female') { false } + expect(accessor).to receive(:selected?).with(subject, :female) { false } expect(subject.female?).to be_falsey end it 'delegates #male! to accessor' do - expect(accessor).to receive(:write).with(subject, 'male') { 0 } + expect(accessor).to receive(:write).with(subject, :male) { 0 } expect(subject.male!).to eq 0 end it 'delegates #female! to accessor' do - expect(accessor).to receive(:write).with(subject, 'female') { 1 } + expect(accessor).to receive(:write).with(subject, :female) { 1 } expect(subject.female!).to eq 1 end end @@ -164,22 +164,22 @@ def generate_enum_spec_extension_for(enum, accessor) end it 'delegates #gender_male? to accessor' do - expect(accessor).to receive(:selected?).with(subject, 'male') { true } + expect(accessor).to receive(:selected?).with(subject, :male) { true } expect(subject.gender_male?).to be_truthy end it 'delegates #gender_female? to accessor' do - expect(accessor).to receive(:selected?).with(subject, 'female') { false } + expect(accessor).to receive(:selected?).with(subject, :female) { false } expect(subject.gender_female?).to be_falsey end it 'delegates #gender_male! to accessor' do - expect(accessor).to receive(:write).with(subject, 'male') { 0 } + expect(accessor).to receive(:write).with(subject, :male) { 0 } expect(subject.gender_male!).to eq 0 end it 'delegates #gender_female! to accessor' do - expect(accessor).to receive(:write).with(subject, 'female') { 1 } + expect(accessor).to receive(:write).with(subject, :female) { 1 } expect(subject.gender_female!).to eq 1 end end diff --git a/spec/simple_enum/enum_spec.rb b/spec/simple_enum/enum_spec.rb index c8e3070..5c16c8a 100644 --- a/spec/simple_enum/enum_spec.rb +++ b/spec/simple_enum/enum_spec.rb @@ -22,29 +22,23 @@ end end - context '#hash' do - subject { described_class.new(:gender, hash).hash } - - it 'returns hash that was set in the constructor' do - expect(subject).to be_a(Hash) - expect(subject.keys).to eq %w{male female} - expect(subject.values).to eq [0, 1] - end - end - context '#keys' do it 'returns the keys in the order added' do - expect(subject.keys).to eq %w{male female} + expect(subject.keys).to eq [:male, :female] end end context '#each_pair (aliased to #each)' do it 'yields twice with #each_pair' do - expect { |b| subject.each_pair(&b) }.to yield_control.exactly(2).times + result = [] + subject.each_pair { |b| result << b } + expect(result).to eq [[:male, 0], [:female, 1]] end it 'yields twice with #each' do - expect { |b| subject.each(&b) }.to yield_control.exactly(2).times + result = [] + subject.each { |b| result << b } + expect(result).to eq [[:male, 0], [:female, 1]] end end From 2f8ceb2bea415de386b50cff5283c4c665f4a2c5 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 29 Apr 2015 16:43:07 +0200 Subject: [PATCH 2/6] use .dev version --- lib/simple_enum/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/simple_enum/version.rb b/lib/simple_enum/version.rb index 3ea2434..00b8a8c 100644 --- a/lib/simple_enum/version.rb +++ b/lib/simple_enum/version.rb @@ -1,5 +1,5 @@ module SimpleEnum # The current `SimpleEnum` version. - VERSION = "2.1.1" + VERSION = "3.0.0.dev" end From 6d8c0e9edd76d0e5a9a355bcf11165e4d694f040 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 29 Apr 2015 16:50:43 +0200 Subject: [PATCH 3/6] skip 1.9 tests and add testing against rails 5.0 --- .travis.yml | 2 +- gemfiles/5.0.gemfile | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 gemfiles/5.0.gemfile diff --git a/.travis.yml b/.travis.yml index b077d90..acd83af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: ruby services: mongodb rvm: - - 1.9.3 - 2.0.0 - 2.1.1 - jruby-19mode @@ -9,6 +8,7 @@ rvm: gemfile: - Gemfile - gemfiles/4.0.gemfile + - gemfiles/5.0.gemfile #matrix: # allow_failures: diff --git a/gemfiles/5.0.gemfile b/gemfiles/5.0.gemfile new file mode 100644 index 0000000..2d8752e --- /dev/null +++ b/gemfiles/5.0.gemfile @@ -0,0 +1,14 @@ +source "http://rubygems.org" + +gem 'activesupport', '5.0.0.alpha', github: 'rails/rails', branch: 'master' +gem 'activerecord', '5.0.0.alpha', github: 'rails/rails', branch: 'master' + +# some development deps +gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby +gem 'sqlite3', platform: :ruby +gem 'bson_ext', platform: :ruby + +# Code coverage on CI only +gem 'codeclimate-test-reporter', group: :test, require: nil + +gemspec path: "../" From b3c1468392198090aaf5239efd55ee8976c76de5 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 29 Apr 2015 20:57:54 +0200 Subject: [PATCH 4/6] corrected gemfile for Rails 5.0 --- .travis.yml | 3 ++- gemfiles/5.0.gemfile | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index acd83af..4b11b72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,8 @@ language: ruby services: mongodb rvm: - 2.0.0 - - 2.1.1 + - 2.2.0 + - ruby-head - jruby-19mode gemfile: diff --git a/gemfiles/5.0.gemfile b/gemfiles/5.0.gemfile index 2d8752e..4e7f597 100644 --- a/gemfiles/5.0.gemfile +++ b/gemfiles/5.0.gemfile @@ -2,6 +2,7 @@ source "http://rubygems.org" gem 'activesupport', '5.0.0.alpha', github: 'rails/rails', branch: 'master' gem 'activerecord', '5.0.0.alpha', github: 'rails/rails', branch: 'master' +gem 'arel', '7.0.0.alpha', github: 'rails/arel', branch: 'master' # some development deps gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby From 457a544abf117217aa7e616c9e931c04e03fee75 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 29 Apr 2015 21:02:46 +0200 Subject: [PATCH 5/6] use dockerized version of travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 4b11b72..b6f2f59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: ruby services: mongodb +sudo: false rvm: - 2.0.0 - 2.2.0 From 7b48bcb7fe43a91984f88a7d6a2621307efb133b Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 30 Apr 2015 10:03:30 +0200 Subject: [PATCH 6/6] JRuby 1.9 / Ruby 2.0 is not supported by Rails 5 --- .travis.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b6f2f59..ea5292f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,10 @@ gemfile: - gemfiles/4.0.gemfile - gemfiles/5.0.gemfile -#matrix: -# allow_failures: -# - rvm: jruby-19mode +matrix: + allow_failures: + - rvm: 2.0.0 + - rvm: jruby-19mode env: global: