lazy_attributes 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/gemfiles/activerecord_3.2.18.gemfile.lock +1 -1
- data/gemfiles/activerecord_4.0.5.gemfile.lock +1 -1
- data/gemfiles/activerecord_4.1.1.gemfile.lock +1 -1
- data/lazy_attributes.gemspec +1 -1
- data/lib/lazy_attributes.rb +8 -11
- data/spec/lazy_attributes_spec.rb +16 -5
- data/spec/spec_helper.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a557716460804140b468ad2933d0d06a72ab5dd
|
4
|
+
data.tar.gz: 85981d9020b694164288aac797a6dcdb90fe8e8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9e3f5a61a9ab12e12bcd1efe857fecef62b7195153ef10601cb772c7ffa9f8d3e2b48e5acffb28decf843e869873c84c13276231a469e8c405cb613f7b1fcc0
|
7
|
+
data.tar.gz: 8568c0a4e5be842cb397a24eeb7e5ec6539df3491cd81824fda866e8f42533ce2a502bfe4299f6c0cb02a127c4eb7891001cdbca2cebb8d4f260f0811a978368
|
data/lazy_attributes.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "lazy_attributes"
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.4'
|
8
8
|
spec.authors = ["Eito Katagiri"]
|
9
9
|
spec.email = ["eitoball@gmail.com"]
|
10
10
|
spec.summary = %q{ActiveRecord plugin to load specified attributes lazyly}
|
data/lib/lazy_attributes.rb
CHANGED
@@ -7,15 +7,6 @@ module LazyAttributes
|
|
7
7
|
self._lazy_attributes = []
|
8
8
|
end
|
9
9
|
|
10
|
-
def reload_but_keep_changes
|
11
|
-
return unless persisted?
|
12
|
-
changes_before_reload = changes.clone
|
13
|
-
reload
|
14
|
-
changes_before_reload.each do |attr_name, values|
|
15
|
-
send("#{attr_name}=", values[1])
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
10
|
module ClassMethods
|
20
11
|
def lazy_attributes(*attrs)
|
21
12
|
if self._lazy_attributes.empty?
|
@@ -29,11 +20,17 @@ module LazyAttributes
|
|
29
20
|
attrs.each do |attr|
|
30
21
|
attr = attr.to_sym
|
31
22
|
define_method(attr) do
|
32
|
-
|
23
|
+
unless has_attribute?(attr)
|
24
|
+
self.class.define_attribute_method(attr)
|
25
|
+
write_attribute(attr, self.class.where(id: self.id).pluck(attr).first)
|
26
|
+
end
|
33
27
|
read_attribute(attr)
|
34
28
|
end
|
35
29
|
define_method(:"#{attr}=") do |val|
|
36
|
-
|
30
|
+
unless has_attribute?(attr)
|
31
|
+
self.class.define_attribute_method(attr)
|
32
|
+
write_attribute(attr, self.class.where(id: self.id).pluck(attr).first)
|
33
|
+
end
|
37
34
|
write_attribute(attr, val)
|
38
35
|
end
|
39
36
|
end
|
@@ -4,11 +4,6 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe LazyAttributes do
|
6
6
|
it '' do
|
7
|
-
expect do
|
8
|
-
User.send(:lazy_attributes, :profile)
|
9
|
-
User.send(:lazy_attributes, :address)
|
10
|
-
end.to_not raise_error
|
11
|
-
|
12
7
|
User.create!(
|
13
8
|
name: 'John',
|
14
9
|
profile: 'This attribute is suppose to be very long',
|
@@ -22,6 +17,22 @@ describe LazyAttributes do
|
|
22
17
|
expect(user.address).to eq('Somewhere over the rainbow')
|
23
18
|
end
|
24
19
|
|
20
|
+
describe '#<lazy_attribute>' do
|
21
|
+
let!(:user) { User.create!(name: 'John', profile: 'My profile...') }
|
22
|
+
|
23
|
+
it { expect(User.find(user.id).profile).to eq('My profile...') }
|
24
|
+
|
25
|
+
context 'association' do
|
26
|
+
before { user.groups.create!(name: 'Group A') }
|
27
|
+
|
28
|
+
it 'should not reload association' do
|
29
|
+
u = User.find(user.id)
|
30
|
+
u.groups.build(name: 'Group B')
|
31
|
+
expect { u.profile }.to_not change { u.groups.size }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
25
36
|
describe '.count' do
|
26
37
|
context 'when no user exists' do
|
27
38
|
subject { User.count }
|
data/spec/spec_helper.rb
CHANGED
@@ -12,11 +12,24 @@ ActiveRecord::Base.establish_connection(
|
|
12
12
|
)
|
13
13
|
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
14
14
|
|
15
|
+
class Group < ActiveRecord::Base
|
16
|
+
belongs_to :user
|
17
|
+
end
|
18
|
+
|
15
19
|
class User < ActiveRecord::Base
|
20
|
+
lazy_attributes :profile
|
21
|
+
lazy_attributes :address
|
22
|
+
|
23
|
+
has_many :groups
|
16
24
|
end
|
17
25
|
|
18
26
|
class CreateTables < ActiveRecord::Migration
|
19
27
|
def up
|
28
|
+
create_table :groups do |t|
|
29
|
+
t.references :user
|
30
|
+
t.string :name
|
31
|
+
end
|
32
|
+
|
20
33
|
create_table :users do |t|
|
21
34
|
t.string :name
|
22
35
|
t.text :profile
|