Skip to content

Commit

Permalink
Merge pull request #61 from khiav223577/fix/cast_values
Browse files Browse the repository at this point in the history
Deal with the parameter handling which was changed in Rails 7.0.6
  • Loading branch information
khiav223577 authored Jul 30, 2023
2 parents 8005714 + 300047e commit c50d1e1
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 40 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- ACTIVE_RECORD
- MONGOID
ruby:
- 2.2
- 2.3
- 2.6
- 2.7
- 3.0
Expand Down Expand Up @@ -78,15 +78,15 @@ jobs:
- gemfile: active_record_52.gemfile
orm: MONGOID
- gemfile: active_record_60.gemfile
ruby: 2.2
ruby: 2.3
- gemfile: active_record_60.gemfile
orm: MONGOID
- gemfile: active_record_61.gemfile
ruby: 2.2
ruby: 2.3
- gemfile: active_record_61.gemfile
orm: MONGOID
- gemfile: active_record_70.gemfile
ruby: 2.2
ruby: 2.3
- gemfile: active_record_70.gemfile
ruby: 2.6
- gemfile: active_record_70.gemfile
Expand All @@ -97,8 +97,6 @@ jobs:
ruby: 3.0
- gemfile: mongoid_54.gemfile
ruby: 3.1
- gemfile: mongoid_73.gemfile
ruby: 2.2
- gemfile: mongoid_54.gemfile
orm: ACTIVE_RECORD
- gemfile: mongoid_64.gemfile
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you have a Rails 3 project, and want to pluck not only one column,
feel free to use this gem and no need to worry about upgrading to Rails 4, 5, 6 in the future will break this.

## Supports
- Ruby 2.2 ~ 2.7, 3.0 ~ 3.1
- Ruby 2.3 ~ 2.7, 3.0 ~ 3.1
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0

## Installation
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/active_record_70.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source 'https://rubygems.org'

gem 'sqlite3', '~> 1.4.1'
gem 'zeitwerk'
gem 'activerecord', '~> 7.0.0'
gem 'activerecord', '~> 7.0.6'
gem 'rails_compatibility', '~> 0.0.7'

gem 'carrierwave', '~> 0.11.0'
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/mongoid_73.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ source 'https://rubygems.org'

gem 'mongoid', '~> 7.3.3'

# We need zeitwerk for autoloading in Rails 7
# But zeitwerk required ruby >= 2.4.4, we cannot test it in ruby 2.3
# So we lock the version below 7
gem 'activesupport', '< 7'

group :test do
gem 'simplecov', '< 0.18'
end
Expand Down
27 changes: 9 additions & 18 deletions lib/pluck_all/models/active_record_extension.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'rails_compatibility/attribute_types'
require 'rails_compatibility/has_include'
require 'rails_compatibility/apply_join_dependency'
require_relative 'patches/deserialize'
require 'rails_compatibility/cast_values'

class ActiveRecord::Relation
def cast_need_columns(column_names, _klass = nil)
Expand All @@ -28,14 +27,10 @@ def select_all(column_names)
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
def pluck_all(*column_names, cast_uploader_url: true)
result = select_all(column_names)
result.map! do |attributes| # This map! behaves different to array#map!
initialized_attributes = klass.initialize_attributes(attributes)
attributes.each do |key, _attribute|
attributes[key] = klass.type_cast_attribute(key, initialized_attributes) # TODO: 現在AS過後的type cast會有一點問題
end
cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
next attributes
end
casted_result = RailsCompatibility.cast_values(klass, result)

casted_result.each{|attributes| cast_carrier_wave_uploader_url(attributes) } if cast_uploader_url
return casted_result
end

private
Expand All @@ -57,14 +52,10 @@ def pluck_all(*column_names, cast_uploader_url: true)
return RailsCompatibility.apply_join_dependency(self).pluck_all(*column_names) if has_include

result = select_all(column_names)
attribute_types = RailsCompatibility.attribute_types(klass)
result.map do |attributes| # This map behaves different to array#map
attributes.each do |key, attribute|
attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) # TODO: 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
end
cast_carrier_wave_uploader_url(attributes) if cast_uploader_url
next attributes
end
casted_result = RailsCompatibility.cast_values(klass, result)

casted_result.each{|attributes| cast_carrier_wave_uploader_url(attributes) } if cast_uploader_url
return casted_result
end

private
Expand Down
13 changes: 0 additions & 13 deletions lib/pluck_all/models/patches/deserialize.rb

This file was deleted.

2 changes: 1 addition & 1 deletion pluck_all.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
}

spec.add_dependency 'activesupport', '>= 3.0.0'
spec.add_dependency 'rails_compatibility', '>= 0.0.8'
spec.add_dependency 'rails_compatibility', '>= 0.0.10'

spec.add_development_dependency 'bundler', '>= 1.17', '< 3.x'
spec.add_development_dependency 'rake', '~> 12.0'
Expand Down
2 changes: 2 additions & 0 deletions test/active_record/support/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
end
end

ActiveRecord::Base.use_yaml_unsafe_load = true if ActiveRecord::Base.method_defined?(:use_yaml_unsafe_load)

users = User.create([
{ name: 'John', email: '[email protected]' },
{ name: 'Pearl', email: '[email protected]', serialized_attribute: { testing: true, deep: { deep: :deep }}},
Expand Down
4 changes: 4 additions & 0 deletions test/mongoid/support/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# frozen_string_literal: true

require 'rails_compatibility/setup_autoload_paths'
RailsCompatibility.setup_autoload_paths [File.expand_path('../models/', __FILE__)]

User.delete_all
User.create(name: 'Pearl Shi', age: 18)
User.create(name: 'Rumble Huang', age: 20)
Expand Down

0 comments on commit c50d1e1

Please sign in to comment.