composite_primary_keys 7.0.9 → 7.0.10

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: 4b5c17b61f779d1923c042647a3a0dea63142517
4
- data.tar.gz: 3861e029662b85465c42b792fa44ffdfc6671c1e
3
+ metadata.gz: bc3c55fbf216f962790b277309e53b78e6359a09
4
+ data.tar.gz: 4c427b0fe5b5eb3d71f955dbc7f377ae04c3d1a8
5
5
  SHA512:
6
- metadata.gz: 97c81fea4acb2ba76454d845f3d7bd009739f1d254a920a9f2332d9976e8f15113fa5e4ef8e3f7805b61e3adfcee39ce7c3866c3948251a6955af8727ddef025
7
- data.tar.gz: 9e92b5600385939973741abc0bb8b27b580cd0ad5931ef1015d8de7fa2cfa5aae38df75a20b68a798a856064bccb214a5a70b3025c7369ffc0ea376f2ec2e044
6
+ metadata.gz: d29164453f49e2a09edcd699012ecc9264df1284875afaa3dba760a47ee9cdf4fa74c2d5792a2fec799006c03288df3f5aff48cd0d78d733275413ac0525598b
7
+ data.tar.gz: 91c39473f95f039f88a3094b75b884982263d2857fdf930c86a26a356972e43f0bf426b300cba6a7b67b26471b246b061eb70ba90f345924394ffac7cd98d163
@@ -1,3 +1,9 @@
1
+ == 7.0.10 (2014-08-07)
2
+
3
+ * Update attribute dirty methods to allow attribute *_was
4
+ fields to be calculated correctly for enum fields (Zachary Salzbank)
5
+ * Update read/write methods to match latest version of ActiveRecord (Charlie Savage)
6
+
1
7
  == 7.0.9 (2014-08-03)
2
8
 
3
9
  * Second attemp at fixing instantiation of has_many records via :includes that use composite keys - this
@@ -10,14 +10,7 @@ module ActiveRecord
10
10
  else
11
11
  attr = attr.to_s
12
12
 
13
- # The attribute already has an unsaved change.
14
- if attribute_changed?(attr)
15
- old = @changed_attributes[attr]
16
- @changed_attributes.delete(attr) unless _field_changed?(attr, old, value)
17
- else
18
- old = clone_attribute_value(:read_attribute, attr)
19
- @changed_attributes[attr] = old if _field_changed?(attr, old, value)
20
- end
13
+ save_changed_attribute(attr, value)
21
14
  end
22
15
 
23
16
  # Carry on.
@@ -1,18 +1,35 @@
1
1
  module ActiveRecord
2
2
  module AttributeMethods
3
3
  module Read
4
- rails_read_attribute = instance_method(:read_attribute)
5
- define_method(:read_attribute) do |attr_name|
4
+ def read_attribute(attr_name)
6
5
  if attr_name.kind_of?(Array)
7
6
  attr_name.map {|name| read_attribute(name)}.to_composite_keys
8
7
  else
9
- rails_read_attribute.bind(self).(attr_name)
8
+ # If it's cached, just return it
9
+ # We use #[] first as a perf optimization for non-nil values. See https://gist.github.com/jonleighton/3552829.
10
+ name = attr_name.to_s
11
+ @attributes_cache[name] || @attributes_cache.fetch(name) {
12
+ column = @column_types_override[name] if @column_types_override
13
+ column ||= @column_types[name]
14
+
15
+ return @attributes.fetch(name) {
16
+ if name == 'id' && self.class.primary_key != name
17
+ read_attribute(self.class.primary_key)
18
+ end
19
+ } unless column
20
+
21
+ value = @attributes.fetch(name) {
22
+ return block_given? ? yield(name) : nil
23
+ }
24
+
25
+ if self.class.cache_attribute?(name)
26
+ @attributes_cache[name] = column.type_cast(value)
27
+ else
28
+ column.type_cast value
29
+ end
30
+ }
10
31
  end
11
32
  end
12
33
  end
13
34
  end
14
- end
15
-
16
- ActiveRecord::Base.class_eval do
17
- alias :[] :read_attribute
18
- end
35
+ end
@@ -1,8 +1,7 @@
1
1
  module ActiveRecord
2
2
  module AttributeMethods
3
3
  module Write
4
- def write_attribute(attr_name, value)
5
- # CPK
4
+ def write_attribute_with_type_cast(attr_name, value, type_cast_method)
6
5
  if attr_name.kind_of?(Array)
7
6
  value = [nil]*attr_name.length if value.nil?
8
7
  unless value.length == attr_name.length
@@ -18,22 +17,19 @@ module ActiveRecord
18
17
  @attributes_cache.delete(attr_name)
19
18
  column = column_for_attribute(attr_name)
20
19
 
21
- unless column || @attributes.has_key?(attr_name)
22
- ActiveSupport::Deprecation.warn(
23
- "You're trying to create an attribute `#{attr_name}'. Writing arbitrary " \
24
- "attributes on a model is deprecated. Please just use `attr_writer` etc."
25
- )
20
+ # If we're dealing with a binary column, write the data to the cache
21
+ # so we don't attempt to typecast multiple times.
22
+ if column && column.binary?
23
+ @attributes_cache[attr_name] = value
24
+ end
25
+
26
+ if column || @attributes.has_key?(attr_name)
27
+ @attributes[attr_name] = send(type_cast_method, column, value)
28
+ else
29
+ raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"
26
30
  end
27
- @attributes[attr_name] = type_cast_attribute_for_write(column, value)
28
31
  end
29
32
  end
30
- alias_method :raw_write_attribute, :write_attribute
31
33
  end
32
34
  end
33
35
  end
34
-
35
- ActiveRecord::Base.class_eval do
36
- alias_method :raw_write_attribute, :write_attribute
37
- alias :[]= :write_attribute
38
- public :[]=
39
- end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 7
4
4
  MINOR = 0
5
- TINY = 9
5
+ TINY = 10
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,5 +1,7 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  belongs_to :person, :polymorphic => true
3
3
  belongs_to :hack
4
+
5
+ enum :shown => [ :true, :false ]
4
6
  end
5
7
 
@@ -108,6 +108,7 @@ create table employees (
108
108
  create table comments (
109
109
  id int not null auto_increment,
110
110
  person_id int default null,
111
+ shown int default null,
111
112
  person_type varchar(100) default null,
112
113
  hack_id int default null,
113
114
  primary key (id)
@@ -116,6 +116,7 @@ create sequence comments_seq start with 1000;
116
116
  create table comments (
117
117
  id number(11) not null primary key,
118
118
  person_id number(11) default null,
119
+ shown number(11) default null,
119
120
  person_type varchar(100) default null,
120
121
  hack_id number(11) default null
121
122
  );
@@ -110,6 +110,7 @@ create table employees (
110
110
  create table comments (
111
111
  id serial not null,
112
112
  person_id int default null,
113
+ shown int default null,
113
114
  person_type varchar(100) default null,
114
115
  hack_id int default null,
115
116
  primary key (id)
@@ -101,6 +101,7 @@ create table employees (
101
101
  create table comments (
102
102
  id integer not null primary key autoincrement,
103
103
  person_id int null,
104
+ shown int null,
104
105
  person_type varchar(100) null,
105
106
  hack_id int null
106
107
  );
@@ -119,6 +119,7 @@ go
119
119
  CREATE TABLE comments (
120
120
  id [int] IDENTITY(1000,1) PRIMARY KEY NOT NULL,
121
121
  person_id [int] NULL,
122
+ shown [int] NULL,
122
123
  person_type varchar(100) NULL,
123
124
  hack_id [int] NULL
124
125
  );
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestEnum < ActiveSupport::TestCase
4
+ fixtures :comments
5
+
6
+ def test_enum_was
7
+ comment = Comment.first
8
+ assert_nil comment.shown
9
+
10
+ comment.shown = :true
11
+ assert_equal 'true', comment.shown
12
+ assert_nil comment.shown_was
13
+
14
+ comment.save
15
+
16
+ comment.shown = :false
17
+ assert_equal 'false', comment.shown
18
+ assert_equal 'true', comment.shown_was
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.9
4
+ version: 7.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-03 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -177,6 +177,7 @@ files:
177
177
  - test/test_delete.rb
178
178
  - test/test_delete_all.rb
179
179
  - test/test_dup.rb
180
+ - test/test_enum.rb
180
181
  - test/test_equal.rb
181
182
  - test/test_exists.rb
182
183
  - test/test_find.rb
@@ -235,6 +236,7 @@ test_files:
235
236
  - test/test_delete.rb
236
237
  - test/test_delete_all.rb
237
238
  - test/test_dup.rb
239
+ - test/test_enum.rb
238
240
  - test/test_equal.rb
239
241
  - test/test_exists.rb
240
242
  - test/test_find.rb