diff --git a/hstore_accessor.gemspec b/hstore_accessor.gemspec index 3fc9236..d9b6510 100644 --- a/hstore_accessor.gemspec +++ b/hstore_accessor.gemspec @@ -31,5 +31,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rubocop" spec.add_development_dependency "shoulda-matchers" - spec.post_install_message = "Please note that the `array` and `hash` types will no longer be supported in version 1.0.0" + spec.post_install_message = "Please note that the `array` and `hash` types are no longer supported in version 1.0.0" end diff --git a/lib/hstore_accessor/active_record_4.2/type_helpers.rb b/lib/hstore_accessor/active_record_4.2/type_helpers.rb index a51fbb2..facf534 100644 --- a/lib/hstore_accessor/active_record_4.2/type_helpers.rb +++ b/lib/hstore_accessor/active_record_4.2/type_helpers.rb @@ -21,7 +21,7 @@ def cast(type, value) return nil if value.nil? case type - when :string, :hash, :array, :decimal + when :string, :decimal value when :integer, :float, :datetime, :date, :boolean TYPES[type].new.type_cast_from_user(value) diff --git a/lib/hstore_accessor/active_record_<_4.2/type_helpers.rb b/lib/hstore_accessor/active_record_<_4.2/type_helpers.rb index e0bbdf1..738891d 100644 --- a/lib/hstore_accessor/active_record_<_4.2/type_helpers.rb +++ b/lib/hstore_accessor/active_record_<_4.2/type_helpers.rb @@ -21,7 +21,7 @@ def cast(type, value) column_class = ActiveRecord::ConnectionAdapters::Column case type - when :string, :hash, :array, :decimal + when :string, :decimal value when :integer column_class.value_to_integer(value) diff --git a/lib/hstore_accessor/macro.rb b/lib/hstore_accessor/macro.rb index 4c0f705..01b5eed 100644 --- a/lib/hstore_accessor/macro.rb +++ b/lib/hstore_accessor/macro.rb @@ -144,9 +144,6 @@ def hstore_accessor(hstore_attribute, fields) when :boolean send(:scope, "is_#{key}", -> { where(eq_query_field, "true") }) send(:scope, "not_#{key}", -> { where(eq_query_field, "false") }) - when :array - send(:scope, "#{key}_eq", -> value { where("#{query_field} = ?", value.join(Serialization::SEPARATOR)) }) - send(:scope, "#{key}_contains", -> value { where("string_to_array(#{query_field}, '#{Serialization::SEPARATOR}') @> string_to_array(?, '#{Serialization::SEPARATOR}')", Array[value].flatten.join(Serialization::SEPARATOR)) }) end end diff --git a/lib/hstore_accessor/serialization.rb b/lib/hstore_accessor/serialization.rb index 07b2512..a45e484 100644 --- a/lib/hstore_accessor/serialization.rb +++ b/lib/hstore_accessor/serialization.rb @@ -3,13 +3,11 @@ module Serialization InvalidDataTypeError = Class.new(StandardError) VALID_TYPES = [ - :array, :boolean, :date, :datetime, :decimal, :float, - :hash, :integer, :string ] @@ -17,24 +15,18 @@ module Serialization DEFAULT_SERIALIZER = ->(value) { value.to_s } DEFAULT_DESERIALIZER = DEFAULT_SERIALIZER - SEPARATOR = "||;||" - SERIALIZERS = { - array: -> value { (value && value.join(SEPARATOR)) || nil }, boolean: -> value { (value.to_s == "true").to_s }, date: -> value { value && value.to_s }, - hash: -> value { (value && value.to_json) || nil }, datetime: -> value { value && value.to_i } } SERIALIZERS.default = DEFAULT_SERIALIZER DESERIALIZERS = { - array: -> value { (value && value.split(SEPARATOR)) || nil }, boolean: -> value { TypeHelpers.cast(:boolean, value) }, date: -> value { value && Date.parse(value) }, decimal: -> value { value && BigDecimal.new(value) }, float: -> value { value && value.to_f }, - hash: -> value { (value && JSON.parse(value)) || nil }, integer: -> value { value && value.to_i }, datetime: -> value { value && Time.at(value.to_i).in_time_zone } } diff --git a/lib/hstore_accessor/version.rb b/lib/hstore_accessor/version.rb index abb2805..343b286 100644 --- a/lib/hstore_accessor/version.rb +++ b/lib/hstore_accessor/version.rb @@ -1,3 +1,3 @@ module HstoreAccessor - VERSION = "0.9.0" + VERSION = "1.0.0" end diff --git a/spec/hstore_accessor_spec.rb b/spec/hstore_accessor_spec.rb index d47e692..62dd298 100644 --- a/spec/hstore_accessor_spec.rb +++ b/spec/hstore_accessor_spec.rb @@ -8,8 +8,6 @@ weight: { data_type: :float, store_key: "w" }, popular: :boolean, build_timestamp: :datetime, - tags: :array, - reviews: :hash, released_at: :date, miles: :decimal } @@ -77,7 +75,7 @@ class FakeModel let!(:timestamp) { Time.now } let!(:datestamp) { Date.today } let(:product) { Product.new } - let(:persisted_product) { Product.create!(color: "green", price: 10, weight: 10.1, tags: %w(tag1 tag2 tag3), popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("9.133790001")) } + let(:persisted_product) { Product.create!(color: "green", price: 10, weight: 10.1, popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("9.133790001")) } FIELDS.keys.each do |field| it "responds with nil when #{field} is not set" do @@ -147,9 +145,9 @@ class FakeModel describe "scopes" do let!(:timestamp) { Time.now } let!(:datestamp) { Date.today } - let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: %w(tag1 tag2 tag3), popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("10.113379001")) } - let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, tags: %w(tag2 tag3 tag4), popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days), miles: BigDecimal.new("20.213379001")) } - let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, tags: %w(tag3 tag4 tag5), popular: true, build_timestamp: timestamp, released_at: datestamp, miles: BigDecimal.new("30.313379001")) } + let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new("10.113379001")) } + let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days), miles: BigDecimal.new("20.213379001")) } + let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, popular: true, build_timestamp: timestamp, released_at: datestamp, miles: BigDecimal.new("30.313379001")) } context "for string fields support" do it "equality" do @@ -223,20 +221,7 @@ class FakeModel end end - context "for array fields support" do - it "equality" do - expect(Product.tags_eq(%w(tag1 tag2 tag3)).to_a).to eq [product_a] - end - - it "contains" do - expect(Product.tags_contains("tag2").to_a).to eq [product_a, product_b] - expect(Product.tags_contains(%w(tag2 tag3)).to_a).to eq [product_a, product_b] - expect(Product.tags_contains(%w(tag1 tag2 tag3)).to_a).to eq [product_a] - expect(Product.tags_contains(%w(tag1 tag2 tag3 tag4)).to_a).to eq [] - end - end - - context "for time fields support" do + context "for datetime fields support" do it "before" do expect(Product.build_timestamp_before(timestamp)).to eq [product_a, product_b] end @@ -323,8 +308,6 @@ def self.it_returns_the_properly_typed_column(type, attribute_name, cast_type_cl it_returns_the_properly_typed_column :datetime, :build_timestamp, ActiveRecord::Type::DateTime it_returns_the_properly_typed_column :date, :released_at, ActiveRecord::Type::Date it_returns_the_properly_typed_column :decimal, :miles, ActiveRecord::Type::Decimal - it_returns_the_properly_typed_column :array, :tags, ActiveRecord::Type::Value - it_returns_the_properly_typed_column :hash, :reviews, ActiveRecord::Type::Value else def self.it_returns_the_properly_typed_column(hstore_type, attribute_name, active_record_type) context "#{hstore_type}" do @@ -388,39 +371,6 @@ def self.it_returns_the_properly_typed_column(hstore_type, attribute_name, activ expect(product.weight).to eq 93.45 end - context "array values" do - it "correctly stores nothing" do - product.tags = nil - product.save - product.reload - expect(product.tags).to be_nil - end - - it "correctly stores strings" do - product.tags = ["household", "living room", "kitchen"] - product.save - product.reload - expect(product.tags).to eq ["household", "living room", "kitchen"] - end - end - - context "hash values" do - it "correctly stores nothing" do - product.reviews = nil - product.save - product.reload - expect(product.reviews).to be_nil - end - - it "correctly stores hash values as json" do - hash = product.reviews = { "user_123" => "4 stars", "user_994" => "3 stars" } - product.save - product.reload - expect(product.reviews).to eq("user_123" => "4 stars", "user_994" => "3 stars") - expect(product.options["reviews"]).to eq(hash.to_json) - end - end - context "multipart values" do it "stores multipart dates correctly" do product.update_attributes!( diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fdc1688..e09b6d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -36,10 +36,8 @@ def create_database t.boolean :boolean_type t.float :float_type t.time :time_type - t.string :array_type, array: true t.date :date_type t.datetime :datetime_type t.decimal :decimal_type - t.hstore :hash_type end end