delegate_associations 0.1.0
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 +7 -0
- data/Rakefile +6 -0
- data/delegate_associations.gemspec +10 -0
- data/lib/delegate_associations.rb +80 -0
- data/lib/delegate_associations/version.rb +3 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9f83dea0633b429592ef960d85c41ba343fc8721
|
4
|
+
data.tar.gz: d4ad9bf510dbdcced0c97b82d18bd4c1cbb3a2a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 676279962ab24726a1bd5dc6e9bf5c1a41ebe1e984dadd2ddbc6a2214ba5289f89cebe5dee38bce38697d8558fd9a53cb9af0e4beb1dfbf3f8bd06742b767816
|
7
|
+
data.tar.gz: cc70a9c15f0661524af4c38e404026839908724b32d0b244f1cf334e3471561a360b8d640946050ea8ef8cf04dfca54f84f527305a6c6d32ca9c987f1a182e2a
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "delegate_associations"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.description = "A gem for delegate attributes and associations of belongs_to and has_one"
|
5
|
+
s.summary = "A gem for delegate attributes and associations of belongs_to and has_one"
|
6
|
+
s.homepage = "https://github.com/Brunomm/delegate_associations"
|
7
|
+
s.author = "Bruno Mucelini Mergen"
|
8
|
+
s.files = Dir["{lib/**/*.rb,README.rdoc,test/**/*.rb,Rakefile,*.gemspec}"]
|
9
|
+
s.email = ["brunomergen@gmail.com"]
|
10
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/version'
|
3
|
+
|
4
|
+
|
5
|
+
module DelegateAssociations
|
6
|
+
|
7
|
+
def delegate_associations(*opts)
|
8
|
+
options = {
|
9
|
+
except: [], only: [], allow_nil: false, to: []
|
10
|
+
}
|
11
|
+
options.update(opts.extract_options!)
|
12
|
+
associations = [options.delete(:to)].flatten.compact.map!(&:to_sym)
|
13
|
+
valid_associations_to(associations)
|
14
|
+
|
15
|
+
exept = [options[:exept]].flatten.compact.map!(&:to_sym)
|
16
|
+
only = [options[:only]].flatten.compact.map!(&:to_sym)
|
17
|
+
exept += delegate_exclude_columns
|
18
|
+
|
19
|
+
associations.each do |association|
|
20
|
+
reflect_on_association(association).klass.reflect_on_all_associations.each do |ass|
|
21
|
+
next unless ass.name.in?(get_deletage_methods(reflect_on_association(association).klass.reflect_on_all_associations.map(&:name), exept, only))
|
22
|
+
if ass.collection?
|
23
|
+
delegate "#{ass.name}", to: association, allow_nil: options[:allow_nil]
|
24
|
+
delegate "#{ass.name}=", to: association, allow_nil: options[:allow_nil]
|
25
|
+
else
|
26
|
+
delegate "#{ass.name}", to: association
|
27
|
+
delegate "#{ass.name}=", to: association
|
28
|
+
delegate "build_#{ass.name}", to: association
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def delegate_attributes(*opts)
|
35
|
+
options = {
|
36
|
+
suffix: ["","=","?","_before_type_cast","_change","_changed?","_was","_will_change!"],
|
37
|
+
except: [], only: [], allow_nil: false, to: []
|
38
|
+
}
|
39
|
+
options.update(opts.extract_options!)
|
40
|
+
associations = [options.delete(:to)].flatten.compact.map!(&:to_sym)
|
41
|
+
|
42
|
+
#Valid if have an option[:to] and if association exists
|
43
|
+
valid_associations_to(associations)
|
44
|
+
|
45
|
+
exept = [options[:exept]].flatten.compact.map!(&:to_sym)
|
46
|
+
only = [options[:only]].flatten.compact.map!(&:to_sym)
|
47
|
+
exept += delegate_exclude_columns
|
48
|
+
|
49
|
+
associations.each do |association|
|
50
|
+
get_deletage_methods(reflect_on_association(association).klass.column_names, exept, only).each do |attribute|
|
51
|
+
puts "#{options[:suffix]}"
|
52
|
+
options[:suffix].each do |sf|
|
53
|
+
delegate "#{attribute}#{sf}", to: association, allow_nil: options[:allow_nil]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def get_deletage_methods(all_options, exept, only)
|
62
|
+
return (all_options.map(&:to_sym)&only)-delegate_exclude_columns if only.any?
|
63
|
+
all_options.map(&:to_sym) - exept
|
64
|
+
end
|
65
|
+
|
66
|
+
def valid_associations_to(associations)
|
67
|
+
raise ":to options can't be blank!" if associations.blank?
|
68
|
+
associations.each do |association|
|
69
|
+
raise "#{name} don't have the association #{association}" unless reflect_on_association(association)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def delegate_exclude_columns
|
74
|
+
[:id, :created_at, :updated_at]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
if defined?(ActiveRecord::Base)
|
79
|
+
ActiveRecord::Base.extend DelegateAssociations
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegate_associations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bruno Mucelini Mergen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem for delegate attributes and associations of belongs_to and has_one
|
14
|
+
email:
|
15
|
+
- brunomergen@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Rakefile
|
21
|
+
- delegate_associations.gemspec
|
22
|
+
- lib/delegate_associations.rb
|
23
|
+
- lib/delegate_associations/version.rb
|
24
|
+
homepage: https://github.com/Brunomm/delegate_associations
|
25
|
+
licenses: []
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: A gem for delegate attributes and associations of belongs_to and has_one
|
47
|
+
test_files: []
|