activerecord-bixformer 0.3.8 → 0.3.9
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2044fd5fafccb188c65c012caa4f5146b96c8ef
|
4
|
+
data.tar.gz: 4377856ad54d04b0effbf9ed70a25b0307ae45df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a561ecbde53c37ed51aca882e86e524b322f298efc9a0609b719e3f2c8aacc4c115bcccf3958207ee56920917c235c3641d755e1718add94ae759a9f22f2f2a9
|
7
|
+
data.tar.gz: 961f0a16e9d034bf1849d601b20c9992b55bdddb090c62f8e2600c4a86ea80875a79daf1697b5d2906868589e1872eeddfd47dded6613a8f5da0cb22292540f7
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Bixformer
|
3
|
+
module Attribute
|
4
|
+
class FormattedForeignKey < ::ActiveRecord::Bixformer::Attribute::Base
|
5
|
+
def initialize(model, attribute_name, options)
|
6
|
+
super
|
7
|
+
|
8
|
+
unless @options[:by]
|
9
|
+
raise ArgumentError.new 'Not configured required options : by'
|
10
|
+
end
|
11
|
+
|
12
|
+
@options[:find_by] ||= if @options[:by].is_a?(::String) || @options[:by].is_a?(::Symbol)
|
13
|
+
@options[:by]
|
14
|
+
end
|
15
|
+
|
16
|
+
unless @options[:find_by]
|
17
|
+
raise ArgumentError.new 'Not configured required options : find_by'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def export(record)
|
22
|
+
association_name = @model.activerecord_constant
|
23
|
+
.reflections.values.find { |r| r.foreign_key == @name }
|
24
|
+
.name
|
25
|
+
|
26
|
+
foreign_record = record.__send__(association_name)
|
27
|
+
|
28
|
+
return nil unless foreign_record
|
29
|
+
|
30
|
+
formatter = @options[:by]
|
31
|
+
|
32
|
+
if formatter.is_a?(::Proc)
|
33
|
+
self.instance_exec foreign_record, &formatter
|
34
|
+
else
|
35
|
+
foreign_record.__send__(formatter)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def import(value)
|
40
|
+
return nil unless value.present?
|
41
|
+
|
42
|
+
find_by = @options[:find_by]
|
43
|
+
scope = @options[:scope] || :all
|
44
|
+
finder = @options[:finder] || :find_by
|
45
|
+
creator = @options[:creator] || :save
|
46
|
+
|
47
|
+
condition = if find_by.is_a?(::Proc)
|
48
|
+
self.instance_exec value, &find_by
|
49
|
+
else
|
50
|
+
{ find_by => value }
|
51
|
+
end
|
52
|
+
|
53
|
+
foreign_record = if scope.is_a?(::Proc)
|
54
|
+
self.instance_exec(&scope).__send__(finder, condition)
|
55
|
+
else
|
56
|
+
foreign_constant.__send__(scope).__send__(finder, condition)
|
57
|
+
end
|
58
|
+
|
59
|
+
if ! foreign_record && @options[:create]
|
60
|
+
foreign_record = if scope.is_a?(::Proc)
|
61
|
+
self.instance_exec(&scope).build(condition)
|
62
|
+
else
|
63
|
+
foreign_constant.__send__(scope).build(condition)
|
64
|
+
end
|
65
|
+
|
66
|
+
if creator.is_a?(::Proc)
|
67
|
+
self.instance_exec foreign_record, &creator
|
68
|
+
else
|
69
|
+
foreign_record.__send__(creator)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
foreign_record&.__send__(foreign_constant.primary_key)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def foreign_constant
|
79
|
+
@foreign_constant ||= @model.activerecord_constant
|
80
|
+
.reflections.values.find { |r| r.foreign_key == @name }
|
81
|
+
.table_name.classify.constantize
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -8,14 +8,15 @@ module ActiveRecord
|
|
8
8
|
autoload :ModelCallback, 'activerecord-bixformer/model_callback'
|
9
9
|
|
10
10
|
module Attribute
|
11
|
-
autoload :Base,
|
12
|
-
autoload :Boolean,
|
13
|
-
autoload :Booletania,
|
14
|
-
autoload :Date,
|
15
|
-
autoload :Enumerize,
|
16
|
-
autoload :
|
17
|
-
autoload :
|
18
|
-
autoload :
|
11
|
+
autoload :Base, 'activerecord-bixformer/attribute/base'
|
12
|
+
autoload :Boolean, 'activerecord-bixformer/attribute/boolean'
|
13
|
+
autoload :Booletania, 'activerecord-bixformer/attribute/booletania'
|
14
|
+
autoload :Date, 'activerecord-bixformer/attribute/date'
|
15
|
+
autoload :Enumerize, 'activerecord-bixformer/attribute/enumerize'
|
16
|
+
autoload :FormattedForeignKey, 'activerecord-bixformer/attribute/formatted_foreign_key'
|
17
|
+
autoload :Override, 'activerecord-bixformer/attribute/override'
|
18
|
+
autoload :String, 'activerecord-bixformer/attribute/string'
|
19
|
+
autoload :Time, 'activerecord-bixformer/attribute/time'
|
19
20
|
end
|
20
21
|
|
21
22
|
module Model
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-bixformer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hiroaki Otsu
|
@@ -228,6 +228,7 @@ files:
|
|
228
228
|
- lib/activerecord-bixformer/attribute/booletania.rb
|
229
229
|
- lib/activerecord-bixformer/attribute/date.rb
|
230
230
|
- lib/activerecord-bixformer/attribute/enumerize.rb
|
231
|
+
- lib/activerecord-bixformer/attribute/formatted_foreign_key.rb
|
231
232
|
- lib/activerecord-bixformer/attribute/override.rb
|
232
233
|
- lib/activerecord-bixformer/attribute/string.rb
|
233
234
|
- lib/activerecord-bixformer/attribute/time.rb
|