From 8d17014d4ab4d07574119a8e813f58c27b2362eb Mon Sep 17 00:00:00 2001 From: Eric Goodwin Date: Mon, 6 Mar 2017 11:14:07 -0800 Subject: [PATCH 1/2] Update cast for Rails 5 support --- lib/preferences/preference_definition.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/preferences/preference_definition.rb b/lib/preferences/preference_definition.rb index b27ae26..566d955 100644 --- a/lib/preferences/preference_definition.rb +++ b/lib/preferences/preference_definition.rb @@ -3,13 +3,13 @@ module Preferences class PreferenceDefinition # The data type for the content stored in this preference type attr_reader :type - + def initialize(name, *args) #:nodoc: options = args.extract_options! options.assert_valid_keys(:default, :group_defaults) - + @type = args.first ? args.first.to_sym : :boolean - + @klass = "ActiveRecord::Type::#{@type.to_s.classify}".constantize.new # Create a column that will be responsible for typecasting @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], @type == :any ? nil : @klass) @@ -19,30 +19,30 @@ def initialize(name, *args) #:nodoc: defaults end end - + # The name of the preference def name @column.name end - + # The default value to use for the preference in case none have been # previously defined def default_value(group = nil) @group_defaults.include?(group) ? @group_defaults[group] : @column.default end - + # Determines whether column backing this preference stores numberic values def number? @column.number? end - + # Typecasts the value based on the type of preference that was defined. # This uses ActiveRecord's typecast functionality so the same rules for # typecasting a model's columns apply here. def type_cast(value) - @type == :any ? value : @column.type_cast_from_database(value) + @type == :any ? value : @klass.deserialize(value) end - + # Typecasts the value to true/false depending on the type of preference def query(value) if !(value = type_cast(value)) From fe5c4359e08b38b2155e8b5d0589c29eb8bffc91 Mon Sep 17 00:00:00 2001 From: Eric Goodwin Date: Mon, 15 Oct 2018 11:18:08 -0700 Subject: [PATCH 2/2] Use Rails 5 Types instead of ActiveRecord::ConnectionAdapters::Column --- lib/preferences/preference_definition.rb | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/preferences/preference_definition.rb b/lib/preferences/preference_definition.rb index 566d955..a404a8c 100644 --- a/lib/preferences/preference_definition.rb +++ b/lib/preferences/preference_definition.rb @@ -2,17 +2,18 @@ module Preferences # Represents the definition of a preference for a particular model class PreferenceDefinition # The data type for the content stored in this preference type - attr_reader :type + attr_reader :type, :name def initialize(name, *args) #:nodoc: options = args.extract_options! options.assert_valid_keys(:default, :group_defaults) - @type = args.first ? args.first.to_sym : :boolean + type = args.first ? args.first.to_sym : :boolean + type = :string if @type == :any - @klass = "ActiveRecord::Type::#{@type.to_s.classify}".constantize.new - # Create a column that will be responsible for typecasting - @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], @type == :any ? nil : @klass) + @type = ActiveRecord::Type.lookup(type) + @name = name + @default = options[:default] @group_defaults = (options[:group_defaults] || {}).inject({}) do |defaults, (group, default)| defaults[group.is_a?(Symbol) ? group.to_s : group] = type_cast(default) @@ -20,27 +21,20 @@ def initialize(name, *args) #:nodoc: end end - # The name of the preference - def name - @column.name - end - # The default value to use for the preference in case none have been # previously defined def default_value(group = nil) - @group_defaults.include?(group) ? @group_defaults[group] : @column.default + @group_defaults.include?(group) ? @group_defaults[group] : @default end # Determines whether column backing this preference stores numberic values def number? - @column.number? + @type.is_a?(ActiveRecord::Type::Integer) end # Typecasts the value based on the type of preference that was defined. - # This uses ActiveRecord's typecast functionality so the same rules for - # typecasting a model's columns apply here. def type_cast(value) - @type == :any ? value : @klass.deserialize(value) + @type.deserialize(value) end # Typecasts the value to true/false depending on the type of preference