Class: ActiveRecord::Bixformer::Attribute::FormattedForeignKey
- Defined in:
- lib/activerecord-bixformer/attribute/formatted_foreign_key.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #export(record) ⇒ Object
- #import(value) ⇒ Object
-
#initialize(model, attribute_name, options) ⇒ FormattedForeignKey
constructor
A new instance of FormattedForeignKey.
- #should_be_included ⇒ Object
Constructor Details
#initialize(model, attribute_name, options) ⇒ FormattedForeignKey
Returns a new instance of FormattedForeignKey.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/activerecord-bixformer/attribute/formatted_foreign_key.rb', line 5 def initialize(model, attribute_name, ) super unless @options[:formatter] raise ArgumentError.new 'Not configured required options : formatter' end @options[:parser] ||= if @options[:formatter].is_a?(::String) || @options[:formatter].is_a?(::Symbol) -> (v) { { @options[:formatter] => v } } end unless @options[:parser] raise ArgumentError.new 'Not configured required options : parser' end end |
Instance Method Details
#export(record) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/activerecord-bixformer/attribute/formatted_foreign_key.rb', line 21 def export(record) association_name = @model.activerecord_constant .reflections.values.find { |r| r.foreign_key == @name } .name foreign_record = record.__send__(association_name) return nil unless foreign_record formatter = @options[:formatter] if formatter.is_a?(::Proc) formatter.call(foreign_record) else foreign_record.__send__(formatter) end end |
#import(value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/activerecord-bixformer/attribute/formatted_foreign_key.rb', line 39 def import(value) return nil unless value.present? parser = @options[:parser] find_by = @options[:find_by] scope = @options[:scope] || :all creator = @options[:creator] || :save condition = if parser.is_a?(::Proc) parser.call(value) else foreign_constant.__send__(parser, value) end return nil unless condition scoped_relation = if scope.is_a?(::Proc) scope.call else foreign_constant.__send__(scope) end foreign_record = if find_by.is_a?(::Proc) find_by.call(scoped_relation, condition) elsif find_by scoped_relation.__send__(find_by, condition) elsif scoped_relation.respond_to?(:find_by) scoped_relation.find_by(condition) else scoped_relation.find do |r| condition.all? { |k, v| r.__send__(k) == v rescue false } end end if ! foreign_record && @options[:create] foreign_record = scoped_relation.build(condition) if creator.is_a?(::Proc) creator.call(foreign_record) else foreign_record.__send__(creator) end end foreign_record&.__send__(foreign_constant.primary_key) end |
#should_be_included ⇒ Object
86 87 88 89 90 |
# File 'lib/activerecord-bixformer/attribute/formatted_foreign_key.rb', line 86 def should_be_included @model.activerecord_constant.reflections.find do |k, r| r.foreign_key == @name && r.class_name.constantize.respond_to?(:table_name) end&.first end |