lazy_attributes 0.0.2 → 0.0.3

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: 6b08ca4b2f37b3915592910de0ada36a9d246049
4
- data.tar.gz: 00a2e4d07c7999591d9abc6b5c5d1dd81af5be5e
3
+ metadata.gz: 91e3b39b32b2e21093801b5c92dee0757adb35b9
4
+ data.tar.gz: f8a6109bb4345cb03fcb7f924a15fb39159508ee
5
5
  SHA512:
6
- metadata.gz: bc2ef56348371beb13886ced937ef44802fc721f9ea0f889c00c9184681c399932a2d7325f1adcc368842c918ccea06bbc9ef1f6c3030454a689c119bd02d38c
7
- data.tar.gz: bc3a80e96a5407302e72fb970d3ba1f97d5a5c5fb02eab1bf8006a71755027b33f9f1836ccb36c85cedc6e1805c8a5a628ce28bf958e1e0d13d8926131f91b6f
6
+ metadata.gz: ee23f503f47d864ccf4a8df06841e8c19931a5d47d565f2a8ff2c2787c4b600e95d66af3f419e387f293a457da8dee7eb1783b382b7ed9614e8bb46ca49988a3
7
+ data.tar.gz: c8848b82eac334456d31cd997a00135377ba6b304f54cdfa9af7c25afc8dfa63329cd6095f4cf5260d4c3afb4be3ce02482f36714abf9bef1a18da9d59f215cf
data/README.md CHANGED
@@ -36,9 +36,17 @@ end
36
36
  ```
37
37
 
38
38
  When model with lazyly-loaded attributes is associated with another
39
- model, you need specify `select` option in association with
39
+ model, you need specify scope for association with
40
40
  '.column_symbols_without_lazy'.
41
41
 
42
+ ```ruby
43
+ class Group < ActiveRecord::Base
44
+ has_many :users, { select(User.column_symbols_without_lazy) }
45
+ end
46
+ ```
47
+
48
+ For Rails 3.2, use `select` option.
49
+
42
50
  ```ruby
43
51
  class Group < ActiveRecord::Base
44
52
  has_many :users, select: User.column_symbols_without_lazy
@@ -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.2'
7
+ spec.version = '0.0.3'
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}
@@ -0,0 +1,17 @@
1
+ module LazyAttributes
2
+ module Relation
3
+ def self.included(mod)
4
+ mod.class_eval do
5
+ alias_method :original_calculate, :calculate
6
+ alias_method :calculate, :calculate_without_select_values
7
+ end
8
+ end
9
+
10
+ def calculate_without_select_values(operation, column_name, options = {})
11
+ original_select_values, self.select_values = self.select_values, []
12
+ original_calculate(operation, column_name, options)
13
+ ensure
14
+ self.select_values = original_select_values
15
+ end
16
+ end
17
+ end
@@ -1,54 +1,59 @@
1
1
  module LazyAttributes
2
- extend ActiveSupport::Concern
2
+ module Base
3
+ extend ActiveSupport::Concern
3
4
 
4
- included do
5
- class_attribute :_lazy_attributes, instance_writer: false
6
- self._lazy_attributes = []
7
- end
8
-
9
- def reload_but_keep_changes
10
- return unless persisted?
11
- changes_before_reload = changes.clone
12
- reload
13
- changes_before_reload.each do |attr_name, values|
14
- send("#{attr_name}=", values[1])
5
+ included do
6
+ class_attribute :_lazy_attributes, instance_writer: false
7
+ self._lazy_attributes = []
15
8
  end
16
- end
17
9
 
18
- module ClassMethods
19
- def lazy_attributes(*attrs)
20
- if self._lazy_attributes.empty?
21
- default_scope do
22
- select(column_names_without_lazy.map do |column_name|
23
- "#{table_name}.#{column_name}"
24
- end)
25
- end
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])
26
16
  end
27
- attrs = attrs.map(&:to_s)
28
- attrs.each do |attr|
29
- attr = attr.to_sym
30
- define_method(attr) do
31
- reload_but_keep_changes unless has_attribute?(attr)
32
- read_attribute(attr)
17
+ end
18
+
19
+ module ClassMethods
20
+ def lazy_attributes(*attrs)
21
+ if self._lazy_attributes.empty?
22
+ default_scope do
23
+ select(column_names_without_lazy.map do |column_name|
24
+ "#{table_name}.#{column_name}"
25
+ end)
26
+ end
33
27
  end
34
- define_method(:"#{attr}=") do |val|
35
- reload_but_keep_changes unless has_attribute?(attr)
36
- write_attribute(attr, val)
28
+ attrs = attrs.map(&:to_s)
29
+ attrs.each do |attr|
30
+ attr = attr.to_sym
31
+ define_method(attr) do
32
+ reload_but_keep_changes unless has_attribute?(attr)
33
+ read_attribute(attr)
34
+ end
35
+ define_method(:"#{attr}=") do |val|
36
+ reload_but_keep_changes unless has_attribute?(attr)
37
+ write_attribute(attr, val)
38
+ end
37
39
  end
40
+ self._lazy_attributes += attrs
38
41
  end
39
- self._lazy_attributes += attrs
40
- end
41
42
 
42
- def column_names_without_lazy
43
- column_names - self._lazy_attributes
44
- end
43
+ def column_names_without_lazy
44
+ column_names - self._lazy_attributes
45
+ end
45
46
 
46
- def column_symbols_without_lazy
47
- column_names_without_lazy.map(&:to_sym)
47
+ def column_symbols_without_lazy
48
+ column_names_without_lazy.map(&:to_sym)
49
+ end
48
50
  end
49
51
  end
50
52
  end
51
53
 
54
+ require_relative 'lazy_attributes/relation'
55
+
52
56
  ActiveSupport.on_load :active_record do
53
- ActiveRecord::Base.send(:include, LazyAttributes)
57
+ ActiveRecord::Base.send(:include, LazyAttributes::Base)
58
+ ActiveRecord::Relation.send(:include, LazyAttributes::Relation)
54
59
  end
@@ -21,4 +21,25 @@ describe LazyAttributes do
21
21
  expect(user.profile).to be_present
22
22
  expect(user.address).to eq('Somewhere over the rainbow')
23
23
  end
24
+
25
+ describe '.count' do
26
+ context 'when no user exists' do
27
+ subject { User.count }
28
+ it { is_expected.to eq(0) }
29
+ end
30
+
31
+ context 'when one user exists' do
32
+ before { User.create! }
33
+
34
+ subject { User.count }
35
+ it { is_expected.to eq(1) }
36
+ end
37
+
38
+ context 'when two users exist' do
39
+ before { 2.times { User.create! } }
40
+
41
+ subject { User.count }
42
+ it { is_expected.to eq(2) }
43
+ end
44
+ end
24
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eito Katagiri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-07 00:00:00.000000000 Z
11
+ date: 2014-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -136,6 +136,7 @@ files:
136
136
  - gemfiles/activerecord_4.1.1.gemfile.lock
137
137
  - lazy_attributes.gemspec
138
138
  - lib/lazy_attributes.rb
139
+ - lib/lazy_attributes/relation.rb
139
140
  - spec/lazy_attributes_spec.rb
140
141
  - spec/spec_helper.rb
141
142
  homepage: ''