active_delegate 0.1.0 → 0.1.1

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: b11efe5cb6d1a9c223450f9da445ab5dd6743128
4
- data.tar.gz: 2c24603c1415aa21e15d85fe405797b268a899a2
3
+ metadata.gz: 2f95ea0f40afa5aabed0b3e54e3b064e764626dc
4
+ data.tar.gz: d3976e39af3730289c55028b76d5f229b50fc4ec
5
5
  SHA512:
6
- metadata.gz: '08899cdfbc043b956ae5d626efb0ed17cc2fdca31d3642bc9dffd5a8bc09a91b9fd4e43e8bb94f441aaa5e196e8aeb92e0bc8967f20774225316d246e6a57a2f'
7
- data.tar.gz: 669b8f94fe5b824cdff5647f0c0f7ab54efed63d763b9534b367d312036b6bb0138eeb92d8bddb5b801b2091c529acd9a5b36ff874ebdce3f67a5df58c3f71f6
6
+ metadata.gz: fc09a5f81b8a25ccd6123fd8b886272cab05b63edb456765afb5ecef04c1b73a83c2bf9300c06a4f83fdedae40048380fe016df659c86aa146d2f0905fc8d462
7
+ data.tar.gz: 22c2241231587b30d7eb2614f4bb672fe1935a7ca437ecaaa4e9ee42d5ac8621cc0a8dfeff5e873af6950b4ace563993b80d17a66061cce5eb3161b98fc9de7c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_delegate (0.1.0)
4
+ active_delegate (0.1.1)
5
5
  i18n (~> 0.8)
6
6
  rails (~> 5.0)
7
7
 
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
- TODO: Write usage instructions here
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
- # Build association method override
86
- def build_association
87
- @model.class.send :define_method, :"#{@options[:to]}" do
88
- super || @model.send(:"build_#{@options[:to]}")
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
- @model.send :define_singleton_method, :"#{@options[:to]}_attribute_names" do
108
- delegated
109
- end
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
- @model.send :define_singleton_method, :"#{@options[:to]}_localized_attribute_names" do
114
- localized
115
- end
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
@@ -1,3 +1,3 @@
1
1
  module ActiveDelegate
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  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.0
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-30 00:00:00.000000000 Z
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails