active_delegate 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +51 -1
- data/lib/active_delegate/attributes.rb +17 -11
- data/lib/active_delegate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f95ea0f40afa5aabed0b3e54e3b064e764626dc
|
4
|
+
data.tar.gz: d3976e39af3730289c55028b76d5f229b50fc4ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc09a5f81b8a25ccd6123fd8b886272cab05b63edb456765afb5ecef04c1b73a83c2bf9300c06a4f83fdedae40048380fe016df659c86aa146d2f0905fc8d462
|
7
|
+
data.tar.gz: 22c2241231587b30d7eb2614f4bb672fe1935a7ca437ecaaa4e9ee42d5ac8621cc0a8dfeff5e873af6950b4ace563993b80d17a66061cce5eb3161b98fc9de7c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
Stores and retrieves delegatable data through attributes on a ActiveRecord class, with support for translatable attributes.
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/active_delegate.svg)](https://badge.fury.io/rb/active_delegate)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/hardpixel/active-delegate/badges/gpa.png)](https://codeclimate.com/github/hardpixel/active-delegate)
|
7
|
+
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add this line to your application's Gemfile:
|
@@ -20,7 +23,54 @@ Or install it yourself as:
|
|
20
23
|
|
21
24
|
## Usage
|
22
25
|
|
23
|
-
|
26
|
+
```ruby
|
27
|
+
class Address < ActiveRecord::Base
|
28
|
+
# columns: :city, :district
|
29
|
+
end
|
30
|
+
|
31
|
+
class Person < ActiveRecord::Base
|
32
|
+
# columns: :name, email, :phone, :address_id
|
33
|
+
|
34
|
+
include ActiveDelegate
|
35
|
+
|
36
|
+
belongs_to :address
|
37
|
+
has_one :user
|
38
|
+
has_many :books
|
39
|
+
|
40
|
+
delegate_attributes to: :address, prefix: true
|
41
|
+
end
|
42
|
+
|
43
|
+
person = Person.new(name: "John", email: 'john@mail.com', address_city: 'Athens', address_district: 'Attiki')
|
44
|
+
|
45
|
+
person.name # 'John'
|
46
|
+
person.address_city # 'Athens'
|
47
|
+
person.address.city # 'Athens'
|
48
|
+
person.address_district # 'Attiki'
|
49
|
+
person.address.district # 'Attiki'
|
50
|
+
|
51
|
+
class User < ActiveRecord::Base
|
52
|
+
# columns: :login, :password, :person_id
|
53
|
+
|
54
|
+
include ActiveDelegate
|
55
|
+
|
56
|
+
belongs_to :person, autosave: true
|
57
|
+
|
58
|
+
delegate_associations to: :person
|
59
|
+
delegate_attributes to: :person
|
60
|
+
end
|
61
|
+
|
62
|
+
user = User.new(login: 'jonian', password: 'passwd', name: "John", email: 'john@mail.com')
|
63
|
+
|
64
|
+
user.name # 'John'
|
65
|
+
user.login # 'jonian'
|
66
|
+
user.user # @user
|
67
|
+
user.books # []
|
68
|
+
|
69
|
+
user.email # 'john@mail.com'
|
70
|
+
user.email? # true
|
71
|
+
user.email_changed? # true
|
72
|
+
user.email_was # nil
|
73
|
+
```
|
24
74
|
|
25
75
|
## Development
|
26
76
|
|
@@ -9,9 +9,9 @@ module ActiveDelegate
|
|
9
9
|
@model = model
|
10
10
|
@options = default_options.merge(options)
|
11
11
|
|
12
|
-
build_association
|
13
12
|
delegate_attributes
|
14
13
|
save_delegated_attributes
|
14
|
+
redefine_build_association
|
15
15
|
end
|
16
16
|
|
17
17
|
private
|
@@ -82,10 +82,16 @@ module ActiveDelegate
|
|
82
82
|
@model.delegate(*delegatable_methods, options)
|
83
83
|
end
|
84
84
|
|
85
|
-
#
|
86
|
-
def
|
87
|
-
|
88
|
-
|
85
|
+
# Redefine build association method
|
86
|
+
def redefine_build_association
|
87
|
+
association_name = @options[:to]
|
88
|
+
|
89
|
+
@model.class_eval do
|
90
|
+
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
91
|
+
def #{association_name}
|
92
|
+
super || send(:build_#{association_name})
|
93
|
+
end
|
94
|
+
EOM
|
89
95
|
end
|
90
96
|
end
|
91
97
|
|
@@ -104,15 +110,15 @@ module ActiveDelegate
|
|
104
110
|
# Save delagated attributes in model class
|
105
111
|
def save_delegated_attributes
|
106
112
|
delegated = prefix_attributes(delegatable_attributes)
|
107
|
-
|
108
|
-
|
109
|
-
|
113
|
+
dl_method = :"#{@options[:to]}_attribute_names"
|
114
|
+
|
115
|
+
@model.send(:define_singleton_method, dl_method) { delegated }
|
110
116
|
|
111
117
|
if @options[:localized].present?
|
112
118
|
localized = prefix_attributes(localized_attributes)
|
113
|
-
|
114
|
-
|
115
|
-
|
119
|
+
lc_method = :"#{@options[:to]}_localized_attribute_names"
|
120
|
+
|
121
|
+
@model.send(:define_singleton_method, lc_method) { localized }
|
116
122
|
end
|
117
123
|
end
|
118
124
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_delegate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonian Guveli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|