Skip to content

Add activerecord-51 gemfile #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gemfile:
- gemfiles/activerecord-412.gemfile
- gemfiles/activerecord-42.gemfile
- gemfiles/activerecord-50.gemfile
- gemfiles/activerecord-51.gemfile
- gemfiles/activerecord-master.gemfile
matrix:
allow_failures:
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Or install it yourself as:
- activerecord-4.1.x
- activerecord-4.2.x
- activerecord-5.0.x
- activerecord-5.1.x

## Usage

Expand Down Expand Up @@ -128,6 +129,7 @@ Support associations is only `belongs_to` and simple `has_many`

1. Fork it ( https://github.com/joker1007/activemodel-associations/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
3. Run tests with `rake spec:all`
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create a new Pull Request
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RSpec::Core::RakeTask.new(:spec)
task :default => :spec

namespace :spec do
%w(activerecord-41 activerecord-412 activerecord-42 activerecord-50 activerecord-master).each do |gemfile|
%w(activerecord-41 activerecord-412 activerecord-42 activerecord-50 activerecord-51 activerecord-master).each do |gemfile|
desc "Run Tests by #{gemfile}.gemfile"
task gemfile do
Bundler.with_clean_env do
Expand All @@ -18,7 +18,7 @@ namespace :spec do

desc "Run All Tests"
task :all do
%w(activerecord-41 activerecord-412 activerecord-42 activerecord-50 activerecord-master).each do |gemfile|
%w(activerecord-41 activerecord-412 activerecord-42 activerecord-50 activerecord-51 activerecord-master).each do |gemfile|
Rake::Task["spec:#{gemfile}"].invoke
end
end
Expand Down
7 changes: 7 additions & 0 deletions gemfiles/activerecord-51.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

gem "activerecord", "~> 5.1.0"
gem "activemodel", "~> 5.1.0"
gem "activesupport", "~> 5.1.0"

gemspec :path => "../"
11 changes: 10 additions & 1 deletion lib/active_model/associations/association_scope_extension.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
module ActiveModel::Associations
module AssociationScopeExtension
if ActiveRecord.version >= Gem::Version.new("5.0.0.beta")
if ActiveRecord.version >= Gem::Version.new("5.1.0")
def add_constraints(scope, owner, refl, chain_head, chain_tail)
if refl.options[:active_model]
target_ids = refl.options[:target_ids]
return scope.where(id: owner[target_ids])
end

super
end
elsif ActiveRecord.version >= Gem::Version.new("5.0.0.beta")
def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tail)
if refl.options[:active_model]
target_ids = refl.options[:target_ids]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def empty?

# full replace simplely
def replace(other_array)
original_target = load_target.dup
other_array.each { |val| raise_on_type_mismatch!(val) }
target_ids = reflection.options[:target_ids]
owner[target_ids] = other_array.map(&:id)

old_records = original_target - other_array
load_target

old_records = target - other_array
old_records.each do |record|
@target.delete(record)
end
Expand Down
22 changes: 17 additions & 5 deletions spec/db/migrate/10_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
if ActiveRecord.version >= Gem::Version.new("5.1.0")
class CreateUsers < ActiveRecord::Migration[4.2]
def change
create_table :users do |t|
t.string :name

t.timestamps
t.timestamps
end
end
end
else
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name

t.timestamps
end
end
end
end
8 changes: 4 additions & 4 deletions spec/lib/active_model/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ class Group
include ActiveModel::Associations

attr_accessor :name
attr_reader :user_ids
attr_reader :id, :user_ids

has_many :users
has_many :users, primary_key: "id"

def [](attr)
self.send(attr)
Expand Down Expand Up @@ -234,8 +234,8 @@ def []=(attr, value)

it "can define different class_name association" do
class DiffClassNameHasManyGroup < Group
attr_reader :member_ids
has_many :members, class_name: "User"
attr_reader :id, :member_ids
has_many :members, class_name: "User", primary_key: "id"
end

user = User.create(name: "joker1007")
Expand Down