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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91e3b39b32b2e21093801b5c92dee0757adb35b9
4
- data.tar.gz: f8a6109bb4345cb03fcb7f924a15fb39159508ee
3
+ metadata.gz: 8a557716460804140b468ad2933d0d06a72ab5dd
4
+ data.tar.gz: 85981d9020b694164288aac797a6dcdb90fe8e8f
5
5
  SHA512:
6
- metadata.gz: ee23f503f47d864ccf4a8df06841e8c19931a5d47d565f2a8ff2c2787c4b600e95d66af3f419e387f293a457da8dee7eb1783b382b7ed9614e8bb46ca49988a3
7
- data.tar.gz: c8848b82eac334456d31cd997a00135377ba6b304f54cdfa9af7c25afc8dfa63329cd6095f4cf5260d4c3afb4be3ce02482f36714abf9bef1a18da9d59f215cf
6
+ metadata.gz: c9e3f5a61a9ab12e12bcd1efe857fecef62b7195153ef10601cb772c7ffa9f8d3e2b48e5acffb28decf843e869873c84c13276231a469e8c405cb613f7b1fcc0
7
+ data.tar.gz: 8568c0a4e5be842cb397a24eeb7e5ec6539df3491cd81824fda866e8f42533ce2a502bfe4299f6c0cb02a127c4eb7891001cdbca2cebb8d4f260f0811a978368
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- lazy_attributes (0.0.2)
4
+ lazy_attributes (0.0.3)
5
5
  activerecord (>= 3.2, < 5)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- lazy_attributes (0.0.2)
4
+ lazy_attributes (0.0.3)
5
5
  activerecord (>= 3.2, < 5)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- lazy_attributes (0.0.2)
4
+ lazy_attributes (0.0.3)
5
5
  activerecord (>= 3.2, < 5)
6
6
 
7
7
  GEM
@@ -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.3'
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}
@@ -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
- reload_but_keep_changes unless has_attribute?(attr)
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
- reload_but_keep_changes unless has_attribute?(attr)
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 }
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eito Katagiri