activerecord-bixformer 0.3.8 → 0.3.9

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: 8cd61e2a16df2901e62146b477572d68b03a0533
4
- data.tar.gz: cd21dc8a9ed1689647b35808f04630480775d822
3
+ metadata.gz: c2044fd5fafccb188c65c012caa4f5146b96c8ef
4
+ data.tar.gz: 4377856ad54d04b0effbf9ed70a25b0307ae45df
5
5
  SHA512:
6
- metadata.gz: 311acc99f8849fbb88446436081a333a3b39f44f6259aeb79e6bbe2cede22945a4acd496b60c0ec4f29636f1610202b770110947f8de7756658ba90af4a3b407
7
- data.tar.gz: 2915ea070d4fbfc379a7ea1c4fad3ebb9cebb5cdc4b01b37b2ef2e295a0d6fb20c248e5e0c38677c07048a1975a493124ae01ed56b56a72569b000f8b18cb5c2
6
+ metadata.gz: a561ecbde53c37ed51aca882e86e524b322f298efc9a0609b719e3f2c8aacc4c115bcccf3958207ee56920917c235c3641d755e1718add94ae759a9f22f2f2a9
7
+ data.tar.gz: 961f0a16e9d034bf1849d601b20c9992b55bdddb090c62f8e2600c4a86ea80875a79daf1697b5d2906868589e1872eeddfd47dded6613a8f5da0cb22292540f7
@@ -6,7 +6,7 @@ module ActiveRecord
6
6
 
7
7
  def initialize(model, attribute_name, options)
8
8
  @model = model
9
- @name = attribute_name
9
+ @name = attribute_name.to_s
10
10
  @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
11
11
  end
12
12
 
@@ -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, '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 :Override, 'activerecord-bixformer/attribute/override'
17
- autoload :String, 'activerecord-bixformer/attribute/string'
18
- autoload :Time, 'activerecord-bixformer/attribute/time'
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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.3.8"
3
+ VERSION = "0.3.9"
4
4
  end
5
5
  end
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.8
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