active_delegate 0.1.1 → 0.1.2

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: 2f95ea0f40afa5aabed0b3e54e3b064e764626dc
4
- data.tar.gz: d3976e39af3730289c55028b76d5f229b50fc4ec
3
+ metadata.gz: 9257eb3266aac4668738613ec5fee51d5bd64fbd
4
+ data.tar.gz: d31d564366686047837729288ef8e61d1d68b609
5
5
  SHA512:
6
- metadata.gz: fc09a5f81b8a25ccd6123fd8b886272cab05b63edb456765afb5ecef04c1b73a83c2bf9300c06a4f83fdedae40048380fe016df659c86aa146d2f0905fc8d462
7
- data.tar.gz: 22c2241231587b30d7eb2614f4bb672fe1935a7ca437ecaaa4e9ee42d5ac8621cc0a8dfeff5e873af6950b4ace563993b80d17a66061cce5eb3161b98fc9de7c
6
+ metadata.gz: 5d080e28edc7ddea35d0985bd401dbe5b7c3eb096638bd63761b6384b71994db51346ca9724de857cd257e4515e359af7d8bd30f8a88f8389263b52b371ac244
7
+ data.tar.gz: 939e98b59a2b65954c7c467c925e5820fd29e99c870f2f2c903b219d468b3c46d1f924bc0ca4e4fd4e062ed83c609003034dd7bdc9c0612b3859d9cb1a746b19
data/Gemfile.lock CHANGED
@@ -2,38 +2,12 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  active_delegate (0.1.1)
5
+ activerecord (~> 5.0)
5
6
  i18n (~> 0.8)
6
- rails (~> 5.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actioncable (5.1.1)
12
- actionpack (= 5.1.1)
13
- nio4r (~> 2.0)
14
- websocket-driver (~> 0.6.1)
15
- actionmailer (5.1.1)
16
- actionpack (= 5.1.1)
17
- actionview (= 5.1.1)
18
- activejob (= 5.1.1)
19
- mail (~> 2.5, >= 2.5.4)
20
- rails-dom-testing (~> 2.0)
21
- actionpack (5.1.1)
22
- actionview (= 5.1.1)
23
- activesupport (= 5.1.1)
24
- rack (~> 2.0)
25
- rack-test (~> 0.6.3)
26
- rails-dom-testing (~> 2.0)
27
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
- actionview (5.1.1)
29
- activesupport (= 5.1.1)
30
- builder (~> 3.1)
31
- erubi (~> 1.4)
32
- rails-dom-testing (~> 2.0)
33
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
- activejob (5.1.1)
35
- activesupport (= 5.1.1)
36
- globalid (>= 0.3.6)
37
11
  activemodel (5.1.1)
38
12
  activesupport (= 5.1.1)
39
13
  activerecord (5.1.1)
@@ -46,66 +20,13 @@ GEM
46
20
  minitest (~> 5.1)
47
21
  tzinfo (~> 1.1)
48
22
  arel (8.0.0)
49
- builder (3.2.3)
50
23
  concurrent-ruby (1.0.5)
51
- erubi (1.6.0)
52
- globalid (0.4.0)
53
- activesupport (>= 4.2.0)
54
- i18n (0.8.1)
55
- loofah (2.0.3)
56
- nokogiri (>= 1.5.9)
57
- mail (2.6.5)
58
- mime-types (>= 1.16, < 4)
59
- method_source (0.8.2)
60
- mime-types (3.1)
61
- mime-types-data (~> 3.2015)
62
- mime-types-data (3.2016.0521)
63
- mini_portile2 (2.1.0)
24
+ i18n (0.8.4)
64
25
  minitest (5.10.2)
65
- nio4r (2.1.0)
66
- nokogiri (1.7.2)
67
- mini_portile2 (~> 2.1.0)
68
- rack (2.0.3)
69
- rack-test (0.6.3)
70
- rack (>= 1.0)
71
- rails (5.1.1)
72
- actioncable (= 5.1.1)
73
- actionmailer (= 5.1.1)
74
- actionpack (= 5.1.1)
75
- actionview (= 5.1.1)
76
- activejob (= 5.1.1)
77
- activemodel (= 5.1.1)
78
- activerecord (= 5.1.1)
79
- activesupport (= 5.1.1)
80
- bundler (>= 1.3.0, < 2.0)
81
- railties (= 5.1.1)
82
- sprockets-rails (>= 2.0.0)
83
- rails-dom-testing (2.0.3)
84
- activesupport (>= 4.2.0)
85
- nokogiri (>= 1.6)
86
- rails-html-sanitizer (1.0.3)
87
- loofah (~> 2.0)
88
- railties (5.1.1)
89
- actionpack (= 5.1.1)
90
- activesupport (= 5.1.1)
91
- method_source
92
- rake (>= 0.8.7)
93
- thor (>= 0.18.1, < 2.0)
94
26
  rake (10.5.0)
95
- sprockets (3.7.1)
96
- concurrent-ruby (~> 1.0)
97
- rack (> 1, < 3)
98
- sprockets-rails (3.2.0)
99
- actionpack (>= 4.0)
100
- activesupport (>= 4.0)
101
- sprockets (>= 3.0.0)
102
- thor (0.19.4)
103
27
  thread_safe (0.3.6)
104
28
  tzinfo (1.2.3)
105
29
  thread_safe (~> 0.1)
106
- websocket-driver (0.6.5)
107
- websocket-extensions (>= 0.1.0)
108
- websocket-extensions (0.1.2)
109
30
 
110
31
  PLATFORMS
111
32
  ruby
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  Stores and retrieves delegatable data through attributes on a ActiveRecord class, with support for translatable attributes.
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/active_delegate.svg)](https://badge.fury.io/rb/active_delegate)
6
+ [![Build Status](https://travis-ci.org/hardpixel/active-delegate.svg?branch=master)](https://travis-ci.org/hardpixel/active-delegate)
6
7
  [![Code Climate](https://codeclimate.com/github/hardpixel/active-delegate/badges/gpa.png)](https://codeclimate.com/github/hardpixel/active-delegate)
7
8
 
8
9
  ## Installation
@@ -37,16 +38,29 @@ class Person < ActiveRecord::Base
37
38
  has_one :user
38
39
  has_many :books
39
40
 
40
- delegate_attributes to: :address, prefix: true
41
+ delegate_attributes to: :address, prefix: true, localized: true
41
42
  end
42
43
 
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'
44
+ params = {
45
+ name: 'John',
46
+ email: 'john@mail.com',
47
+ address_city: 'Athens',
48
+ address_city_el: 'Αθήνα',
49
+ address_district: 'Attiki',
50
+ address_district_el: 'Αττική'
51
+ }
52
+
53
+ person = Person.new(params)
54
+
55
+ person.name # 'John'
56
+ person.address_city # 'Athens'
57
+ person.address.city # 'Athens'
58
+ person.address_city_el # 'Αθήνα'
59
+ person.address.city_el # 'Αθήνα'
60
+ person.address_district # 'Attiki'
61
+ person.address.district # 'Attiki'
62
+ person.address_district_el # 'Αττική'
63
+ person.address.district_el # 'Αττική'
50
64
 
51
65
  class User < ActiveRecord::Base
52
66
  # columns: :login, :password, :person_id
@@ -59,7 +73,14 @@ class User < ActiveRecord::Base
59
73
  delegate_attributes to: :person
60
74
  end
61
75
 
62
- user = User.new(login: 'jonian', password: 'passwd', name: "John", email: 'john@mail.com')
76
+ params = {
77
+ login: 'jonian',
78
+ password: 'passwd',
79
+ name: 'John',
80
+ email: 'john@mail.com'
81
+ }
82
+
83
+ user = User.new(params)
63
84
 
64
85
  user.name # 'John'
65
86
  user.login # 'jonian'
@@ -48,11 +48,18 @@ module ActiveDelegate
48
48
  [:id, :created_at, :updated_at] + poly_attr.to_a
49
49
  end
50
50
 
51
+ # Get method attributes
52
+ def association_valid_methods(attributes)
53
+ assoc_methods = association_class.public_instance_methods
54
+ attributes.to_a.select { |a| a.in? assoc_methods }
55
+ end
56
+
51
57
  # Get delegatable attributes
52
58
  def delegatable_attributes
53
59
  attributes = association_attribute_names.map(&:to_sym)
54
60
  attributes = attributes & @options[:only].to_a if @options[:only].present?
55
61
  attributes = attributes - @options[:except].to_a if @options[:except].present?
62
+ attributes = attributes + association_valid_methods(@options[:only])
56
63
  attributes = attributes - default_excluded_attributes
57
64
 
58
65
  attributes.map(&:to_sym)
@@ -84,12 +91,12 @@ module ActiveDelegate
84
91
 
85
92
  # Redefine build association method
86
93
  def redefine_build_association
87
- association_name = @options[:to]
94
+ assoc_name = @options[:to]
88
95
 
89
96
  @model.class_eval do
90
97
  class_eval <<-EOM, __FILE__, __LINE__ + 1
91
- def #{association_name}
92
- super || send(:build_#{association_name})
98
+ def #{assoc_name}
99
+ super || send(:build_#{assoc_name})
93
100
  end
94
101
  EOM
95
102
  end
@@ -109,8 +116,9 @@ module ActiveDelegate
109
116
 
110
117
  # Save delagated attributes in model class
111
118
  def save_delegated_attributes
112
- delegated = prefix_attributes(delegatable_attributes)
113
119
  dl_method = :"#{@options[:to]}_attribute_names"
120
+ delegated = prefix_attributes(delegatable_attributes)
121
+ delegated = @model.try(dl_method).to_a.concat(delegated)
114
122
 
115
123
  @model.send(:define_singleton_method, dl_method) { delegated }
116
124
 
@@ -1,3 +1,3 @@
1
1
  module ActiveDelegate
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -4,6 +4,8 @@ module ActiveDelegate
4
4
  autoload :Associations, 'active_delegate/associations'
5
5
  autoload :Attributes, 'active_delegate/attributes'
6
6
 
7
+ require "active_record"
8
+
7
9
  class << self
8
10
  def included(model_class)
9
11
  model_class.extend self
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_delegate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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-31 00:00:00.000000000 Z
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  requirements: []
122
122
  rubyforge_project:
123
- rubygems_version: 2.6.11
123
+ rubygems_version: 2.6.13
124
124
  signing_key:
125
125
  specification_version: 4
126
126
  summary: Delegate ActiveRecord model attributes and associations