Module: ArSync::ModelBase::ClassMethods
- Defined in:
- lib/ar_sync/class_methods.rb
Instance Method Summary collapse
- #_each_sync_child(&block) ⇒ Object
- #_each_sync_parent(&block) ⇒ Object
- #_initialize_sync_callbacks ⇒ Object
- #_sync_child_info(name) ⇒ Object
- #_sync_children_info ⇒ Object
- #_sync_define(name, serializer_data_block: nil, **option, &data_block) ⇒ Object
- #_sync_has_many(name, order: :asc, limit: nil, preload: nil, association: nil, **option, &data_block) ⇒ Object
- #_sync_parents_info ⇒ Object
- #_sync_self? ⇒ Boolean
- #sync_collection(name) ⇒ Object
- #sync_define_collection(name, limit: nil, order: :asc) ⇒ Object
- #sync_has_data(*names, **option, &data_block) ⇒ Object
- #sync_has_many(name, **option, &data_block) ⇒ Object
- #sync_has_one(name, **option, &data_block) ⇒ Object
- #sync_parent(parent, inverse_of:, only_to: nil, watch: nil) ⇒ Object
Instance Method Details
#_each_sync_child(&block) ⇒ Object
28 29 30 31 |
# File 'lib/ar_sync/class_methods.rb', line 28 def _each_sync_child(&block) _sync_children_info.each(&block) superclass._each_sync_child(&block) if superclass < ActiveRecord::Base end |
#_each_sync_parent(&block) ⇒ Object
23 24 25 26 |
# File 'lib/ar_sync/class_methods.rb', line 23 def _each_sync_parent(&block) _sync_parents_info.each(&block) superclass._each_sync_parent(&block) if superclass < ActiveRecord::Base end |
#_initialize_sync_callbacks ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ar_sync/class_methods.rb', line 113 def _initialize_sync_callbacks return if instance_variable_defined? '@_sync_callbacks_initialized' @_sync_callbacks_initialized = true mod = Module.new do def _write_attribute(attr_name, value) self.class.default_scoped.scoping do @_sync_watch_values_before_mutation ||= _sync_current_watch_values @_sync_parents_info_before_mutation ||= _sync_current_parents_info @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info end super attr_name, value end end prepend mod attr_reader :_sync_parents_info_before_mutation, :_sync_belongs_to_info_before_mutation, :_sync_watch_values_before_mutation _sync_define :id _sync_define :sync_keys, type: [:string] do |current_user| ArSync.sync_keys self, current_user end _sync_define :defaults, namespace: :sync do |current_user| { sync_keys: ArSync.sync_keys(self, current_user) } end before_destroy do @_sync_parents_info_before_mutation ||= _sync_current_parents_info @_sync_watch_values_before_mutation ||= _sync_current_watch_values @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info end before_save on: :create do @_sync_parents_info_before_mutation ||= {} @_sync_watch_values_before_mutation ||= {} @_sync_belongs_to_info_before_mutation ||= {} end i[create update destroy].each do |action| after_commit on: action do next if ArSync.skip_notification? self.class.default_scoped.scoping { _sync_notify action } @_sync_watch_values_before_mutation = nil @_sync_parents_info_before_mutation = nil @_sync_belongs_to_info_before_mutation = nil end end end |
#_sync_child_info(name) ⇒ Object
17 18 19 20 21 |
# File 'lib/ar_sync/class_methods.rb', line 17 def _sync_child_info(name) info = _sync_children_info[name] return info if info superclass._sync_child_info name if superclass < ActiveRecord::Base end |
#_sync_children_info ⇒ Object
13 14 15 |
# File 'lib/ar_sync/class_methods.rb', line 13 def _sync_children_info @_sync_children_info ||= {} end |
#_sync_define(name, serializer_data_block: nil, **option, &data_block) ⇒ Object
101 102 103 104 105 |
# File 'lib/ar_sync/class_methods.rb', line 101 def _sync_define(name, serializer_data_block: nil, **option, &data_block) _initialize_sync_callbacks serializer_field name, **option, &(serializer_data_block || data_block) unless _serializer_field_info name serializer_field name, **option, namespace: :sync, &data_block end |
#_sync_has_many(name, order: :asc, limit: nil, preload: nil, association: nil, **option, &data_block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ar_sync/class_methods.rb', line 63 def _sync_has_many(name, order: :asc, limit: nil, preload: nil, association: nil, **option, &data_block) raise "order not in [:asc, :desc] : #{order}" unless i[asc desc].include? order if data_block.nil? && preload.nil? underscore_name = name.to_s.underscore.to_sym preload = lambda do |records, _context, params| ArSerializer::Field.preload_association( self, records, association || underscore_name, order: (!limit && params && params[:order]) || order, limit: [params && params[:limit]&.to_i, limit].compact.min ) end data_block = lambda do |preloaded, _context, params| records = preloaded ? preloaded[id] || [] : send(name) next records unless limit || order == :asc ArSync::CollectionWithOrder.new( records, order: (!limit && params && params[:order]) || order, limit: [params && params[:limit]&.to_i, limit].compact.min ) end serializer_data_block = lambda do |preloaded, _context, _params| preloaded ? preloaded[id] || [] : send(name) end params_type = { limit?: :int, order?: [{ :* => %w[asc desc] }, 'asc', 'desc'] } else params_type = {} end _sync_define( name, serializer_data_block: serializer_data_block, preload: preload, association: association, params_type: params_type, **option, &data_block ) end |
#_sync_parents_info ⇒ Object
9 10 11 |
# File 'lib/ar_sync/class_methods.rb', line 9 def _sync_parents_info @_sync_parents_info ||= [] end |
#_sync_self? ⇒ Boolean
5 6 7 |
# File 'lib/ar_sync/class_methods.rb', line 5 def _sync_self? instance_variable_defined? '@_sync_self' end |
#sync_collection(name) ⇒ Object
41 42 43 |
# File 'lib/ar_sync/class_methods.rb', line 41 def sync_collection(name) ArSync::Collection.find self, name end |
#sync_define_collection(name, limit: nil, order: :asc) ⇒ Object
107 108 109 110 111 |
# File 'lib/ar_sync/class_methods.rb', line 107 def sync_define_collection(name, limit: nil, order: :asc) _initialize_sync_callbacks collection = ArSync::Collection.new self, name, limit: limit, order: order sync_parent collection, inverse_of: [self, name] end |
#sync_has_data(*names, **option, &data_block) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/ar_sync/class_methods.rb', line 45 def sync_has_data(*names, **option, &data_block) @_sync_self = true names.each do |name| _sync_children_info[name] = nil _sync_define name, option, &data_block end end |
#sync_has_many(name, **option, &data_block) ⇒ Object
53 54 55 56 |
# File 'lib/ar_sync/class_methods.rb', line 53 def sync_has_many(name, **option, &data_block) _sync_children_info[name] = [:many, option, data_block] _sync_has_many name, option, &data_block end |
#sync_has_one(name, **option, &data_block) ⇒ Object
58 59 60 61 |
# File 'lib/ar_sync/class_methods.rb', line 58 def sync_has_one(name, **option, &data_block) _sync_children_info[name] = [:one, option, data_block] _sync_define name, option, &data_block end |
#sync_parent(parent, inverse_of:, only_to: nil, watch: nil) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/ar_sync/class_methods.rb', line 33 def sync_parent(parent, inverse_of:, only_to: nil, watch: nil) _initialize_sync_callbacks _sync_parents_info << [ parent, { inverse_name: inverse_of, only_to: only_to, watch: watch } ] end |