Sha256: 352d0f2ce3f4a8059bfd8c3ffd3e28968a84a6b6d40eb5da604b86677e78361a

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require_relative 'field'

class ArSync::Collection
  attr_reader :klass, :name, :limit, :order
  def initialize(klass, name, limit: nil, order: nil)
    @klass = klass
    @name = name
    @limit = limit
    @order = order
    self.class.defined_collections[[klass, name]] = self
    define_singleton_method(name) { to_a }
  end

  def to_a
    all = klass.all
    all = all.order id: order if order
    all = all.limit limit if limit
    all
  end

  def self.defined_collections
    @defined_collections ||= {}
  end

  def self.find(klass, name)
    defined_collections[[klass, name]]
  end

  def _sync_notify_child_changed(_name, _to_user); end

  def _sync_notify_child_added(child, _name, to_user)
    ArSync.sync_send to: self, action: :add, model: child, path: :collection, to_user: to_user
  end

  def _sync_notify_child_removed(child, _name, to_user, _owned)
    ArSync.sync_send to: self, action: :remove, model: child, path: :collection, to_user: to_user
  end

  def self._sync_children_info
    @sync_children_info ||= {}
  end

  def self._sync_child_info(key)
    _sync_children_info[key]
  end
end

class ArSync::CollectionWithOrder < ArSerializer::CompositeValue
  def initialize(records, order:, limit:)
    @records = records
    @order = { mode: order, limit: limit }
  end

  def ar_serializer_build_sub_calls
    values = @records.map { {} }
    [{ order: @order, collection: values }, @records.zip(values)]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ar_sync-1.0.5 lib/ar_sync/collection.rb
ar_sync-1.0.4 lib/ar_sync/collection.rb
ar_sync-1.0.3 lib/ar_sync/collection.rb
ar_sync-1.0.2 lib/ar_sync/collection.rb