active_record-acts_as 1.0.0.pre → 1.0.0.rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e30b0cf3d5bebc22c5d3e9a63f81bced680e579
4
- data.tar.gz: 11a3af36ca6331fe13cf84a962e6ba1728f9572a
3
+ metadata.gz: c137b5172e254e835624861325efa82e6ca2e609
4
+ data.tar.gz: c1836efae6258976e635b8232d1c7654b3e33838
5
5
  SHA512:
6
- metadata.gz: 64f7cc31a9033eb4954981f24b6ae5edd5ac8c73ca05de836d2a01d62b5f8c59c41983caa91cf0e76b365a35e7d32ed96f5e02567ad125ef2d23be046ee0f496
7
- data.tar.gz: 97c3f506fa35cfb4fa391b16f5ca3636db662b1f6963b8fc74d4f9e4fe02f8b50585f31142efe1c6f99b9aa7f06aafc222b8ebedc55f2e0b28f4f085cb21d82e
6
+ metadata.gz: 17f92b735d3520d7a66da1f75b6e7455963df6d1721557faceaca82c98949fbaab4110588f8254b94268dbeb30ddba1d6f1226424f1cf5edfb3e9e8db61effe2
7
+ data.tar.gz: 14117fe50dfde347a40a607ac783eca19aedbc46fe3c1a4d66a1bbdb594deb26c4e185d584fa68200c39ad020e7be4af6cc3908fe77356ebe346510015a51815
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in active_record-acts_as.gemspec
4
4
  gemspec
5
+
6
+ gem 'coveralls', require: false
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
+ [![Gem Version](https://badge.fury.io/rb/active_record-acts_as.svg)](http://badge.fury.io/rb/active_record-acts_as)
1
2
  [![Build Status](https://travis-ci.org/hzamani/active_record-acts_as.svg)](https://travis-ci.org/hzamani/active_record-acts_as)
2
3
  [![Code Climate](https://codeclimate.com/github/hzamani/active_record-acts_as.png)](https://codeclimate.com/github/hzamani/active_record-acts_as)
4
+ [![Coverage Status](https://coveralls.io/repos/hzamani/active_record-acts_as/badge.png)](https://coveralls.io/r/hzamani/active_record-acts_as)
3
5
 
4
6
  # ActiveRecord::ActsAs
5
7
 
@@ -148,6 +150,12 @@ acts_as :person, -> { includes(:friends) }
148
150
  Make sure you know what you are doing when ovrewriting `polymorphic` option.
149
151
 
150
152
 
153
+ ## Migrating from acts_as_relation
154
+
155
+ Replace `acts_as_superclass` in models with `actable` and if you where using
156
+ `:as_relation_superclass` option on `create_table` remove it and use `t.actable` on column definitions.
157
+
158
+
151
159
  ## Contributing
152
160
 
153
161
  1. Fork it ( https://github.com/hzamani/active_record-acts_as/fork )
@@ -20,7 +20,7 @@ module ActiveRecord
20
20
  protected :actable_must_be_valid
21
21
 
22
22
  def read_attribute(attr_name, *args, &block)
23
- if attribute_method?(attr_name)
23
+ if attribute_method?(attr_name.to_s)
24
24
  super
25
25
  else
26
26
  acting_as.read_attribute(attr_name, *args, &block)
@@ -28,7 +28,7 @@ module ActiveRecord
28
28
  end
29
29
 
30
30
  def write_attribute(attr_name, value, *args, &block)
31
- if attribute_method?(attr_name)
31
+ if attribute_method?(attr_name.to_s)
32
32
  super
33
33
  else
34
34
  acting_as.send(:write_attribute, attr_name, value, *args, &block)
@@ -40,10 +40,19 @@ module ActiveRecord
40
40
  acting_as.attributes.except(acting_as_reflection.type, acting_as_reflection.foreign_key).merge(super)
41
41
  end
42
42
 
43
+ def attribute_names
44
+ super | (acting_as.attribute_names - [acting_as_reflection.type, acting_as_reflection.foreign_key])
45
+ end
46
+
47
+
43
48
  def respond_to?(name, include_private = false)
44
49
  super || acting_as.respond_to?(name)
45
50
  end
46
51
 
52
+ def dup
53
+ super.acting_as = acting_as.dup
54
+ end
55
+
47
56
  def method_missing(method, *args, &block)
48
57
  if acting_as.respond_to?(method)
49
58
  acting_as.send(method, *args, &block)
@@ -20,4 +20,16 @@ module ActiveRecord
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ class Relation
25
+ alias_method :scope_for_create_without_acting_as, :scope_for_create
26
+
27
+ def scope_for_create
28
+ @scope_for_create ||= if acting_as?
29
+ where_values_hash.merge(where_values_hash(acting_as_model.table_name)).merge(create_with_value)
30
+ else
31
+ where_values_hash.merge(create_with_value)
32
+ end
33
+ end
34
+ end
23
35
  end
@@ -17,14 +17,16 @@ module ActiveRecord
17
17
  cattr_reader(:acting_as_name) { name.to_s }
18
18
  cattr_reader(:acting_as_model) { name.to_s.camelize.constantize }
19
19
  class_eval "def acting_as() #{name} || build_#{name} end"
20
+ alias_method :acting_as=, "#{name}=".to_sym
20
21
 
21
22
  include ActsAs::InstanceMethods
22
23
  extend ActsAs::Querying
23
24
  end
24
25
 
25
26
  def acting_as?(other = nil)
26
- if respond_to? :acting_as_name
27
- other.nil? || acting_as_name == other.to_s.underscore
27
+ if respond_to?(:acting_as_reflection) &&
28
+ acting_as_reflection.is_a?(ActiveRecord::Reflection::AssociationReflection)
29
+ other.nil? || acting_as_reflection.name.to_s == other.to_s.underscore
28
30
  else
29
31
  false
30
32
  end
@@ -37,10 +39,17 @@ module ActiveRecord
37
39
  def actable(options = {})
38
40
  name = options.delete(:as) || :actable
39
41
 
40
- belongs_to name, {polymorphic: true, dependent: :delete, autosave: true}.merge(options)
42
+ reflections = belongs_to name, {polymorphic: true, dependent: :delete, autosave: true}.merge(options)
43
+
44
+ cattr_reader(:actable_reflection) { reflections[name] }
41
45
 
42
46
  alias_method :specific, name
43
47
  end
48
+
49
+ def actable?
50
+ respond_to?(:actable_reflection) &&
51
+ actable_reflection.is_a?(ActiveRecord::Reflection::AssociationReflection)
52
+ end
44
53
  end
45
54
  end
46
55
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  module ActiveRecord
3
3
  module ActsAs
4
- VERSION = "1.0.0.pre"
4
+ VERSION = "1.0.0.rc"
5
5
  end
6
6
  end
data/spec/actable_spec.rb CHANGED
@@ -13,6 +13,16 @@ RSpec.describe "ActiveRecord::Base subclass with #actable" do
13
13
  expect(association).to be_polymorphic
14
14
  end
15
15
 
16
+ describe "#actable?" do
17
+ it "returns true for actable models" do
18
+ expect(Product.actable?).to be true
19
+ end
20
+
21
+ it "returns false for none actable models" do
22
+ expect(Pen.actable?).to be false
23
+ end
24
+ end
25
+
16
26
  describe ".specific" do
17
27
  it "return specific submodel" do
18
28
  pen.save
data/spec/acts_as_spec.rb CHANGED
@@ -67,6 +67,23 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
67
67
  end
68
68
  end
69
69
 
70
+ describe "#acting_as=" do
71
+ it "sets acts_as model" do
72
+ product = Product.new(name: 'new product', price: 0.99)
73
+ pen = Pen.new
74
+ pen.acting_as = product
75
+ expect(pen.acting_as).to eq(product)
76
+ end
77
+ end
78
+
79
+ describe "#dup" do
80
+ it "duplicates actable model as well" do
81
+ p = pen.dup
82
+ expect(p.name).to eq('pen')
83
+ expect(p.price).to eq(0.8)
84
+ end
85
+ end
86
+
70
87
  it "have supermodel attributes accessible on creation" do
71
88
  expect{Pen.create(pen_attributes)}.to_not raise_error
72
89
  end
@@ -128,6 +145,10 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
128
145
  it "includes supermodel attributes in .to_json responce" do
129
146
  expect(pen.to_json).to eq('{"id":null,"name":"pen","price":0.8,"store_id":null,"color":"red"}')
130
147
  end
148
+
149
+ it "includes supermodel attribute names in .attribute_names responce" do
150
+ expect(pen.attribute_names).to include("id", "color", "name", "price", "store_id")
151
+ end
131
152
  end
132
153
 
133
154
  context "Querying" do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
1
3
 
2
4
  RSpec.configure do |config|
3
5
  config.disable_monkey_patching!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-acts_as
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre
4
+ version: 1.0.0.rc
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hassan Zamani