activerecord-alias_association 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 011bc02e72e0db134d8c0204749287f4d8cb6872
4
- data.tar.gz: b35a50c435fd7aa244c63e9a2c755af3e07e98c4
3
+ metadata.gz: 83dc002c673c1efbe64f4d5a005cfb49aab34ed4
4
+ data.tar.gz: d8ab3301efe4f68fde9bc7d3e83dca09a82afcb3
5
5
  SHA512:
6
- metadata.gz: 5ab87875baaa55c8cb46a866558a79b24c9ddd0d3659878b90bebbdc8c251433281b4746d57a98e949a37d8d523cacec09fc391c7aeb720757f6859cfa1cc0db
7
- data.tar.gz: dae4c4c2de3154935fe60481a56e279ac1085cc4aead4432875b52597fa95fab7018c9dbc097d1fe8cc4e74e91e57f82e00c82774a4ca3d2d9f5494682029bdf
6
+ metadata.gz: 0bf1517d700cea8f58e2a373a318fd7d8327d4d728204863daf4d5afa6f9678e004e2e27471fae778122a888c7da9701e6653882e16c166f610c05e63a408b84
7
+ data.tar.gz: 3070307c4e2675825d8c5de85ddace49b7d3aa2f4080f48e10859f18ec1daf03ad2cdabc553301e72d05290e6cefe17b65288307e095e9e8c5f44123d2f99da1
data/.travis.yml CHANGED
@@ -22,10 +22,6 @@ matrix:
22
22
 
23
23
  - rvm: 2.3.8
24
24
  gemfile: gemfiles/activerecord.5.2.x.gemfile
25
- - rvm: 2.3.8
26
- gemfile: gemfiles/activerecord.4.2.x.gemfile
27
- - rvm: 2.3.8
28
- gemfile: gemfiles/activerecord.4.1.x.gemfile
29
25
 
30
26
  - rvm: 2.2.10
31
27
  gemfile: gemfiles/activerecord.5.2.x.gemfile
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/Jun0kada/activerecord-alias_association.svg?branch=master)](https://travis-ci.org/Jun0kada/activerecord-alias_association)
4
4
 
5
5
  ActiveRecord Association alias
6
- `belongs_to`, `has_one`, `has_many`, `has_many through`
6
+ `belongs_to`, `has_one`, `has_many`, `has_and_belongs_to_many`
7
7
 
8
8
  ## Installation
9
9
 
@@ -25,7 +25,7 @@ Or install it yourself as:
25
25
 
26
26
  ```ruby
27
27
  class User < ActiveRecord::Base
28
- belongs_to :team, alias: :organization
28
+ belongs_to :team, alias: [:organization, :club]
29
29
 
30
30
  has_one :profile, alias: :info
31
31
 
@@ -33,6 +33,8 @@ class User < ActiveRecord::Base
33
33
 
34
34
  has_many :post_images, through: :users, source: :images, alias: :article_images
35
35
 
36
+ has_and_belongs_to_many :tags, alias: :categories
37
+
36
38
  # or
37
39
 
38
40
  belongs_to :team
@@ -46,14 +48,39 @@ class User < ActiveRecord::Base
46
48
 
47
49
  has_many :post_images, through: :users, source: :images
48
50
  alias_association :article_images, :post_images
51
+
52
+ has_and_belongs_to_many :tags
53
+ alias_association :categories, :tags
49
54
  end
50
55
  ```
51
56
 
52
- ## Development
57
+ ### Accessors & Constructors
53
58
 
54
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+ ```ruby
60
+ user = User.first
61
+
62
+ # belongs_to, has_one
63
+ user.organization
64
+ user.organization=
65
+ user.build_organization
66
+ user.create_organization
67
+ user.create_organization!
68
+ user.reload_organization
69
+
70
+ # nas_many, has_and_belongs_to_many
71
+ user.posts
72
+ user.posts=
73
+ user.post_ids
74
+ user.post_ids=
75
+ ```
55
76
 
56
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+ ### ActiveRecord QueryMethods
78
+
79
+ ```ruby
80
+ User.includes(:organization, :posts, :tags)
81
+ User.preload(:organization, :posts, :tags)
82
+ User.eager_load(:organization, :posts, :tags)
83
+ ```
57
84
 
58
85
  ## Contributing
59
86
 
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ['lib']
23
23
 
24
- spec.add_dependency 'activerecord', '>= 4.1.0'
24
+ spec.add_dependency 'activerecord', '>= 5.0.0'
25
25
 
26
26
  spec.add_development_dependency 'bundler'
27
27
  spec.add_development_dependency 'rake'
@@ -9,7 +9,7 @@ module ActiveRecord
9
9
  module AssociationBuilderExtension
10
10
  def self.build(model, reflection)
11
11
  Array.wrap(reflection.options[:alias]).each do |new_name|
12
- model.send(:alias_association, new_name, reflection.name)
12
+ model.send(:alias_association, new_name, reflection.name, reflection)
13
13
  end
14
14
  end
15
15
 
@@ -19,11 +19,37 @@ module ActiveRecord
19
19
  end
20
20
 
21
21
  module ClassMethods
22
- def alias_association(new_name, old_name)
22
+ def alias_association(new_name, old_name, reflection = nil)
23
+ reflection ||= reflect_on_association(old_name)
24
+
25
+ raise NoMethodError, "undefined #{old_name} association for #{self}" unless reflection
26
+
23
27
  generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
24
28
  alias_method :#{new_name}, :#{old_name}
25
29
  alias_method :#{new_name}=, :#{old_name}=
26
30
  CODE
31
+
32
+ case reflection.macro
33
+ when :belongs_to, :has_one
34
+ generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
35
+ alias_method "reload_#{new_name}", "reload_#{old_name}"
36
+ CODE
37
+
38
+ unless reflection.polymorphic?
39
+ generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
40
+ alias_method "build_#{new_name}", "build_#{old_name}"
41
+ alias_method "create_#{new_name}", "create_#{old_name}"
42
+ alias_method "create_#{new_name}!", "create_#{old_name}!"
43
+ CODE
44
+ end
45
+ when :has_many, :has_and_belongs_to_many
46
+ generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
47
+ alias_method "#{new_name.to_s.singularize}_ids", "#{old_name.to_s.singularize}_ids"
48
+ alias_method "#{new_name.to_s.singularize}_ids=", "#{old_name.to_s.singularize}_ids="
49
+ CODE
50
+ else
51
+ raise "Unsupported #{reflection.macro} reflection."
52
+ end
27
53
  end
28
54
 
29
55
  def reflect_on_association(association)
@@ -0,0 +1,47 @@
1
+ module ActiveRecord
2
+ module Extension
3
+ module Associations
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def has_and_belongs_to_many(name, scope = nil, **options, &extension)
8
+ habtm_reflection = ActiveRecord::Reflection::HasAndBelongsToManyReflection.new(name, scope, options, self)
9
+
10
+ builder = ActiveRecord::Associations::Builder::HasAndBelongsToMany.new name, self, options
11
+
12
+ join_model = builder.through_model
13
+
14
+ const_set join_model.name, join_model
15
+ private_constant join_model.name
16
+
17
+ middle_reflection = builder.middle_reflection join_model
18
+
19
+ ActiveRecord::Associations::Builder::HasMany.define_callbacks self, middle_reflection
20
+ ActiveRecord::Reflection.add_reflection self, middle_reflection.name, middle_reflection
21
+ middle_reflection.parent_reflection = habtm_reflection
22
+
23
+ include Module.new {
24
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
25
+ def destroy_associations
26
+ association(:#{middle_reflection.name}).delete_all(:delete_all)
27
+ association(:#{name}).reset
28
+ super
29
+ end
30
+ RUBY
31
+ }
32
+
33
+ hm_options = {}
34
+ hm_options[:through] = middle_reflection.name
35
+ hm_options[:source] = join_model.right_reflection.name
36
+
37
+ [:alias, :before_add, :after_add, :before_remove, :after_remove, :autosave, :validate, :join_table, :class_name, :extend].each do |k|
38
+ hm_options[k] = options[k] if options.key? k
39
+ end
40
+
41
+ has_many name, scope, hm_options, &extension
42
+ _reflections[name.to_s].parent_reflection = habtm_reflection
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,9 +1,11 @@
1
1
  require 'activerecord/alias_association/version'
2
2
  require 'active_record'
3
3
  require 'active_record/alias_association'
4
+ require 'active_record/extension/associations'
4
5
 
5
6
  module ActiveRecord
6
7
  class Base
7
8
  include ActiveRecord::AliasAssociation
9
+ include ActiveRecord::Extension::Associations
8
10
  end
9
11
  end
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module AliasAssociation
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-alias_association
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jun0kada
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-05 00:00:00.000000000 Z
11
+ date: 2019-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.0
19
+ version: 5.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.0
26
+ version: 5.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -98,12 +98,11 @@ files:
98
98
  - activerecord-alias_association.gemspec
99
99
  - bin/console
100
100
  - bin/setup
101
- - gemfiles/activerecord.4.1.x.gemfile
102
- - gemfiles/activerecord.4.2.x.gemfile
103
101
  - gemfiles/activerecord.5.0.x.gemfile
104
102
  - gemfiles/activerecord.5.1.x.gemfile
105
103
  - gemfiles/activerecord.5.2.x.gemfile
106
104
  - lib/active_record/alias_association.rb
105
+ - lib/active_record/extension/associations.rb
107
106
  - lib/activerecord-alias_association.rb
108
107
  - lib/activerecord/alias_association/version.rb
109
108
  homepage: https://github.com/Jun0kada/activerecord-alias_association
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'activerecord', '~> 4.1.0'
6
- gem 'sqlite3', '~> 1.3.6'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec path: '..'
4
-
5
- gem 'activerecord', '~> 4.2.0'
6
- gem 'sqlite3', '~> 1.3.6'