diff --git a/Appraisals b/Appraisals index 90179a1..5876a44 100644 --- a/Appraisals +++ b/Appraisals @@ -25,3 +25,20 @@ appraise "rails-6" do gem "activesupport", "6.0.0" gem "activerecord", "6.0.0" end + +appraise "rails-7.0.4" do + gem "activesupport", "7.0.4" + gem "activerecord", "7.0.4" +end + +appraise "rails-7.1" do + gem "activesupport", "7.1.0" + gem "activerecord", "7.1.0" + gem "sqlite3", "~> 1.4" +end + +appraise "rails-7.2" do + gem "activesupport", "~> 7.2.0" + gem "activerecord", "~> 7.2.0" + gem "sqlite3", "~> 2.0" +end diff --git a/README.md b/README.md index 9745e31..b0971db 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Or install it yourself as: preferences do - preference :taxable data_type: :boolean, required: true + preference :taxable data_type: :boolean, required: true, default: true preference :vat_no required: false preference :max_invoice_items data_type: :integer diff --git a/gemfiles/rails_7.0.4.gemfile b/gemfiles/rails_7.0.4.gemfile new file mode 100644 index 0000000..c363a4d --- /dev/null +++ b/gemfiles/rails_7.0.4.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activesupport", "7.0.4" +gem "activerecord", "7.0.4" + +gemspec path: "../" diff --git a/gemfiles/rails_7.0.4.gemfile.lock b/gemfiles/rails_7.0.4.gemfile.lock new file mode 100644 index 0000000..26aa944 --- /dev/null +++ b/gemfiles/rails_7.0.4.gemfile.lock @@ -0,0 +1,145 @@ +PATH + remote: .. + specs: + serial_preference (1.3.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) + +GEM + remote: https://rubygems.org/ + specs: + actionpack (7.0.4) + actionview (= 7.0.4) + activesupport (= 7.0.4) + rack (~> 2.0, >= 2.2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actionview (7.0.4) + activesupport (= 7.0.4) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activemodel (7.0.4) + activesupport (= 7.0.4) + activerecord (7.0.4) + activemodel (= 7.0.4) + activesupport (= 7.0.4) + activesupport (7.0.4) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + appraisal (2.4.1) + bundler + rake + thor (>= 0.14.0) + builder (3.2.4) + concurrent-ruby (1.1.10) + crass (1.0.6) + date (3.5.1) + debug (1.11.1) + irb (~> 1.10) + reline (>= 0.3.8) + diff-lcs (1.5.0) + erb (6.0.1) + erubi (1.12.0) + i18n (1.12.0) + concurrent-ruby (~> 1.0) + io-console (0.8.2) + irb (1.16.0) + pp (>= 0.6.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + loofah (2.19.1) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + method_source (1.0.0) + minitest (5.17.0) + nokogiri (1.14.0-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.14.0-x86_64-linux) + racc (~> 1.4) + pp (0.6.3) + prettyprint + prettyprint (0.2.0) + psych (5.3.1) + date + stringio + racc (1.6.2) + rack (2.2.6) + rack-test (2.0.2) + rack (>= 1.3) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.4.4) + loofah (~> 2.19, >= 2.19.1) + railties (7.0.4) + actionpack (= 7.0.4) + activesupport (= 7.0.4) + method_source + rake (>= 12.2) + thor (~> 1.0) + zeitwerk (~> 2.5) + rake (13.0.6) + rdoc (7.1.0) + erb + psych (>= 4.0.0) + tsort + reline (0.6.3) + io-console (~> 0.5) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.0) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.3) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-rails (6.0.1) + actionpack (>= 6.1) + activesupport (>= 6.1) + railties (>= 6.1) + rspec-core (~> 3.11) + rspec-expectations (~> 3.11) + rspec-mocks (~> 3.11) + rspec-support (~> 3.11) + rspec-support (3.12.0) + shoulda (4.0.0) + shoulda-context (~> 2.0) + shoulda-matchers (~> 4.0) + shoulda-context (2.0.0) + shoulda-matchers (4.5.1) + activesupport (>= 4.2.0) + sqlite3 (1.6.0-x86_64-darwin) + sqlite3 (1.6.0-x86_64-linux) + stringio (3.2.0) + thor (1.2.1) + tsort (0.2.0) + tzinfo (2.0.5) + concurrent-ruby (~> 1.0) + zeitwerk (2.6.6) + +PLATFORMS + x86_64-darwin-22 + x86_64-linux + +DEPENDENCIES + activerecord (= 7.0.4) + activesupport (= 7.0.4) + appraisal + debug + rspec (>= 3.0.0) + rspec-rails + serial_preference! + shoulda + sqlite3 + +BUNDLED WITH + 2.4.3 diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile new file mode 100644 index 0000000..136eccc --- /dev/null +++ b/gemfiles/rails_7.1.gemfile @@ -0,0 +1,9 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activesupport", "7.1.0" +gem "activerecord", "7.1.0" +gem "sqlite3", "~> 1.4" + +gemspec path: "../" diff --git a/gemfiles/rails_7.1.gemfile.lock b/gemfiles/rails_7.1.gemfile.lock new file mode 100644 index 0000000..d0968c7 --- /dev/null +++ b/gemfiles/rails_7.1.gemfile.lock @@ -0,0 +1,164 @@ +PATH + remote: .. + specs: + serial_preference (1.3.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) + +GEM + remote: https://rubygems.org/ + specs: + actionpack (7.1.0) + actionview (= 7.1.0) + activesupport (= 7.1.0) + nokogiri (>= 1.8.5) + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + actionview (7.1.0) + activesupport (= 7.1.0) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activemodel (7.1.0) + activesupport (= 7.1.0) + activerecord (7.1.0) + activemodel (= 7.1.0) + activesupport (= 7.1.0) + timeout (>= 0.4.0) + activesupport (7.1.0) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + appraisal (2.5.0) + bundler + rake + thor (>= 0.14.0) + base64 (0.3.0) + bigdecimal (4.0.1) + builder (3.3.0) + concurrent-ruby (1.3.6) + connection_pool (3.0.2) + crass (1.0.6) + date (3.5.1) + debug (1.11.1) + irb (~> 1.10) + reline (>= 0.3.8) + diff-lcs (1.6.2) + drb (2.2.3) + erb (6.0.1) + erubi (1.13.1) + i18n (1.14.8) + concurrent-ruby (~> 1.0) + io-console (0.8.2) + irb (1.16.0) + pp (>= 0.6.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + loofah (2.25.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + minitest (6.0.1) + prism (~> 1.5) + mutex_m (0.3.0) + nokogiri (1.19.0-x86_64-darwin) + racc (~> 1.4) + pp (0.6.3) + prettyprint + prettyprint (0.2.0) + prism (1.8.0) + psych (5.3.1) + date + stringio + racc (1.8.1) + rack (3.2.4) + rack-session (2.1.1) + base64 (>= 0.1.0) + rack (>= 3.0.0) + rack-test (2.2.0) + rack (>= 1.3) + rackup (2.3.1) + rack (>= 3) + rails-dom-testing (2.3.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.2) + loofah (~> 2.21) + nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) + railties (7.1.0) + actionpack (= 7.1.0) + activesupport (= 7.1.0) + irb + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rake (13.3.1) + rdoc (7.1.0) + erb + psych (>= 4.0.0) + tsort + reline (0.6.3) + io-console (~> 0.5) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.6) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.7) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-rails (7.1.1) + actionpack (>= 7.0) + activesupport (>= 7.0) + railties (>= 7.0) + rspec-core (~> 3.13) + rspec-expectations (~> 3.13) + rspec-mocks (~> 3.13) + rspec-support (~> 3.13) + rspec-support (3.13.6) + shoulda (4.0.0) + shoulda-context (~> 2.0) + shoulda-matchers (~> 4.0) + shoulda-context (2.0.0) + shoulda-matchers (4.5.1) + activesupport (>= 4.2.0) + sqlite3 (1.6.0-x86_64-darwin) + stringio (3.2.0) + thor (1.5.0) + timeout (0.6.0) + tsort (0.2.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + zeitwerk (2.7.4) + +PLATFORMS + x86_64-darwin-22 + +DEPENDENCIES + activerecord (= 7.1.0) + activesupport (= 7.1.0) + appraisal + debug + rspec (>= 3.0.0) + rspec-rails + serial_preference! + shoulda + sqlite3 (~> 1.4) + +BUNDLED WITH + 2.3.26 diff --git a/gemfiles/rails_7.2.gemfile b/gemfiles/rails_7.2.gemfile new file mode 100644 index 0000000..0584516 --- /dev/null +++ b/gemfiles/rails_7.2.gemfile @@ -0,0 +1,9 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "activesupport", "~> 7.2.0" +gem "activerecord", "~> 7.2.0" +gem "sqlite3", "~> 2.0" + +gemspec path: "../" diff --git a/gemfiles/rails_7.2.gemfile.lock b/gemfiles/rails_7.2.gemfile.lock new file mode 100644 index 0000000..5f9d3ae --- /dev/null +++ b/gemfiles/rails_7.2.gemfile.lock @@ -0,0 +1,176 @@ +PATH + remote: .. + specs: + serial_preference (1.3.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) + +GEM + remote: https://rubygems.org/ + specs: + actionpack (7.2.3) + actionview (= 7.2.3) + activesupport (= 7.2.3) + cgi + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4, < 3.3) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + useragent (~> 0.16) + actionview (7.2.3) + activesupport (= 7.2.3) + builder (~> 3.1) + cgi + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activemodel (7.2.3) + activesupport (= 7.2.3) + activerecord (7.2.3) + activemodel (= 7.2.3) + activesupport (= 7.2.3) + timeout (>= 0.4.0) + activesupport (7.2.3) + base64 + benchmark (>= 0.3) + bigdecimal + concurrent-ruby (~> 1.0, >= 1.3.1) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + logger (>= 1.4.2) + minitest (>= 5.1) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + appraisal (2.5.0) + bundler + rake + thor (>= 0.14.0) + base64 (0.3.0) + benchmark (0.5.0) + bigdecimal (4.0.1) + builder (3.3.0) + cgi (0.5.1) + concurrent-ruby (1.3.6) + connection_pool (3.0.2) + crass (1.0.6) + date (3.5.1) + debug (1.11.1) + irb (~> 1.10) + reline (>= 0.3.8) + diff-lcs (1.6.2) + drb (2.2.3) + erb (6.0.1) + erubi (1.13.1) + i18n (1.14.8) + concurrent-ruby (~> 1.0) + io-console (0.8.2) + irb (1.16.0) + pp (>= 0.6.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + logger (1.7.0) + loofah (2.25.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + minitest (6.0.1) + prism (~> 1.5) + nokogiri (1.19.0-x86_64-darwin) + racc (~> 1.4) + pp (0.6.3) + prettyprint + prettyprint (0.2.0) + prism (1.8.0) + psych (5.3.1) + date + stringio + racc (1.8.1) + rack (3.2.4) + rack-session (2.1.1) + base64 (>= 0.1.0) + rack (>= 3.0.0) + rack-test (2.2.0) + rack (>= 1.3) + rackup (2.3.1) + rack (>= 3) + rails-dom-testing (2.3.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.2) + loofah (~> 2.21) + nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) + railties (7.2.3) + actionpack (= 7.2.3) + activesupport (= 7.2.3) + cgi + irb (~> 1.13) + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + tsort (>= 0.2) + zeitwerk (~> 2.6) + rake (13.3.1) + rdoc (7.1.0) + erb + psych (>= 4.0.0) + tsort + reline (0.6.3) + io-console (~> 0.5) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.6) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.7) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-rails (8.0.2) + actionpack (>= 7.2) + activesupport (>= 7.2) + railties (>= 7.2) + rspec-core (~> 3.13) + rspec-expectations (~> 3.13) + rspec-mocks (~> 3.13) + rspec-support (~> 3.13) + rspec-support (3.13.6) + securerandom (0.4.1) + shoulda (4.0.0) + shoulda-context (~> 2.0) + shoulda-matchers (~> 4.0) + shoulda-context (2.0.0) + shoulda-matchers (4.5.1) + activesupport (>= 4.2.0) + sqlite3 (2.9.0-x86_64-darwin) + stringio (3.2.0) + thor (1.5.0) + timeout (0.6.0) + tsort (0.2.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + useragent (0.16.11) + zeitwerk (2.7.4) + +PLATFORMS + x86_64-darwin-22 + +DEPENDENCIES + activerecord (~> 7.2.0) + activesupport (~> 7.2.0) + appraisal + debug + rspec (>= 3.0.0) + rspec-rails + serial_preference! + shoulda + sqlite3 (~> 2.0) + +BUNDLED WITH + 2.3.26 diff --git a/lib/serial_preference.rb b/lib/serial_preference.rb index 28adf2e..5e39d1e 100644 --- a/lib/serial_preference.rb +++ b/lib/serial_preference.rb @@ -2,5 +2,6 @@ require 'active_record' require "serial_preference/version" require 'serial_preference/preference_definition' +require 'serial_preference/safe_yaml_coder' require "serial_preference/preferenzer" require "serial_preference/has_preference_map" diff --git a/lib/serial_preference/has_preference_map.rb b/lib/serial_preference/has_preference_map.rb index e255335..6c25c15 100644 --- a/lib/serial_preference/has_preference_map.rb +++ b/lib/serial_preference/has_preference_map.rb @@ -32,7 +32,11 @@ def preference_names private def build_preference_definitions - serialize self._preferences_attribute, Hash + if serialize_supports_coder? + serialize self._preferences_attribute, coder: SerialPreference::SafeYamlCoder, type: Hash + else + serialize self._preferences_attribute, Hash + end _preference_map.all_preference_definitions.each do |preference| @@ -55,6 +59,11 @@ def build_preference_definitions end end + + def serialize_supports_coder? + ActiveRecord::Base.respond_to?(:serialize) && + ActiveRecord::Base.method(:serialize).parameters.any? { |p| p.include?(:coder) } + end end protected @@ -71,6 +80,5 @@ def write_preference_attribute(store_attribute, key, value) attribute[key.to_sym] = value end end - end end diff --git a/lib/serial_preference/preference_definition.rb b/lib/serial_preference/preference_definition.rb index c165170..77601ea 100644 --- a/lib/serial_preference/preference_definition.rb +++ b/lib/serial_preference/preference_definition.rb @@ -8,20 +8,20 @@ class PreferenceDefinition def initialize(name,*args) opts = args.extract_options! self.name = name.to_s - opts.assert_valid_keys(:data_type,:default,:required,:field_type) + opts.assert_valid_keys(:data_type, :default, :required, :field_type) self.data_type = @type = opts[:data_type] || :string - @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, opts[:default], column_type(@type)) + @column = rails_below_6? ? ActiveRecord::ConnectionAdapters::Column.new(name.to_s, opts[:default], column_type(@type)) : column_type(@type) self.default = opts[:default] self.required = !!opts[:required] self.field_type = opts[:field_type] end def name - @column.name + @column.respond_to?(:name) ? @column.name : @name end def default_value - @column.default + @column.respond_to?(:default) ? @column.default : @default end def required? @@ -37,7 +37,7 @@ def boolean? end def type_cast(value) - v = @column.type_cast(value) + v = @column.respond_to?(:cast) ? @column.cast(value) : @column.type_cast(value) v.nil? ? default_value : v end @@ -61,25 +61,37 @@ def value(v) case data_type when :string, :password v.to_s - when :integer + when :integer v.respond_to?(:to_i) ? v.to_i : nil when :float, :real v.respond_to?(:to_f) ? v.to_f : nil when :boolean - return false if v == 0 - return false if v == "" - return false if v == nil - return false if v.to_s.downcase == "false" - return false if v == "0" - return false if v.to_s.downcase == "no" - !!v + !!normalize_boolean(v) else nil end end end + private + def column_type(type) + if rails_below_6? + column_type_below_6(type) + else + column_type_greater_6(type) + end + end + + def column_type_greater_6(type) + begin + ActiveModel::Type.lookup(type) + rescue ArgumentError + ActiveModel::Type::String.new + end + end + + def column_type_below_6(type) if greater_or_equal_rails_42? case type when :boolean @@ -98,8 +110,22 @@ def column_type(type) end end + def normalize_boolean(v) + if rails_below_6? + return false if ["", "0", "false", "no"].include?(v.to_s.downcase) + v + else + return false if !v || (v.is_a?(String) && (v.downcase == "no")) + ActiveModel::Type::Boolean.new.cast(v.to_s.downcase) + end + end + def greater_or_equal_rails_42? ActiveRecord::VERSION::MAJOR > 4 || (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) end + + def rails_below_6? + ActiveRecord::VERSION::MAJOR < 6 + end end end diff --git a/lib/serial_preference/safe_yaml_coder.rb b/lib/serial_preference/safe_yaml_coder.rb new file mode 100644 index 0000000..8346100 --- /dev/null +++ b/lib/serial_preference/safe_yaml_coder.rb @@ -0,0 +1,15 @@ +module SerialPreference + module SafeYamlCoder + def self.dump(obj) + YAML.dump(obj) + end + + def self.load(yaml) + return {} if yaml.nil? + + Psych.safe_load(yaml, permitted_classes: [Symbol], aliases: true) || {} + rescue Psych::Exception, TypeError + YAML.load(yaml) || {} + end + end +end diff --git a/serial_preference.gemspec b/serial_preference.gemspec index 29fccb0..7946846 100644 --- a/serial_preference.gemspec +++ b/serial_preference.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency "activerecord", ">= 3.0.0" gem.add_development_dependency 'sqlite3' + gem.add_development_dependency 'debug' gem.add_development_dependency 'rspec', ">= 3.0.0" gem.add_development_dependency 'rspec-rails' gem.add_development_dependency "shoulda" diff --git a/spec/preference_definition_spec.rb b/spec/preference_definition_spec.rb index c132314..45eb2b2 100644 --- a/spec/preference_definition_spec.rb +++ b/spec/preference_definition_spec.rb @@ -179,6 +179,69 @@ end end + context "boolean? behaviour" do + it "should be boolean when data_type is boolean" do + p = described_class.new("whatever",{data_type: :boolean}) + expect(p.boolean?).to be_truthy + end + + it "should not be boolean for non boolean data types" do + [:string, :integer, :float, :decimal, :password, :xyz].each do |dt| + expect(described_class.new("whatever",{data_type: dt}).boolean?).to be_falsey + end + end + end + + context "default_value behaviour" do + it "should return nil when no default is set" do + expect(@blank_pref.default_value).to be_nil + end + + it "should return cast default value for boolean" do + p = described_class.new("flag",{default: true, data_type: :boolean}) + expect(p.default_value).to be_truthy + end + end + + context "normalize_boolean behaviour" do + before do + @bool = described_class.new("flag",{data_type: :boolean}) + end + + it "should treat 'yes' as true" do + expect(@bool.send(:normalize_boolean, "yes")).to be_truthy + end + + it "should treat 'no' as false" do + expect(@bool.send(:normalize_boolean, "no")).to be_falsey + end + + it "should treat numeric zero as false" do + expect(@bool.send(:normalize_boolean, 0)).to be_falsey + end + + it "should treat numeric '0' as false" do + expect(@bool.send(:normalize_boolean, '0')).to be_falsey + end + end + + context "value method edge cases" do + it "should return default when value is nil and default exists" do + p = described_class.new("color",{default: "red"}) + expect(p.value(nil)).to eq("red") + end + + it "should handle boolean strings properly" do + p = described_class.new("flag",{data_type: :boolean}) + expect(p.value("true")).to be_truthy + expect(p.value("false")).to be_falsey + end + + it "should not coerce non boolean types into boolean" do + p = described_class.new("mode",{data_type: :string}) + expect(p.value("false")).to eq("false") + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ab4e8f8..2b8f025 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,11 +4,13 @@ require 'active_record' require 'yaml' require 'rspec' +require 'debug' require 'shoulda' require "serial_preference/version" require 'serial_preference/preference_definition' require "serial_preference/preferenzer" require "serial_preference/has_preference_map" +require 'serial_preference/safe_yaml_coder' FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures") config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))