lazy_attributes 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -1
- data/lazy_attributes.gemspec +1 -1
- data/lib/lazy_attributes/relation.rb +17 -0
- data/lib/lazy_attributes.rb +43 -38
- data/spec/lazy_attributes_spec.rb +21 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91e3b39b32b2e21093801b5c92dee0757adb35b9
|
4
|
+
data.tar.gz: f8a6109bb4345cb03fcb7f924a15fb39159508ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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.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
|
data/lib/lazy_attributes.rb
CHANGED
@@ -1,54 +1,59 @@
|
|
1
1
|
module LazyAttributes
|
2
|
-
|
2
|
+
module Base
|
3
|
+
extend ActiveSupport::Concern
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
43
|
+
def column_names_without_lazy
|
44
|
+
column_names - self._lazy_attributes
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
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.
|
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-
|
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: ''
|