class Syncano::ActiveRecord::Base

Class for integrating ActiveRecord functionality

Public Class Methods

all() click to toggle source

Gets collection with all objects @return [Array]

# File lib/syncano/active_record/base.rb, line 22
def self.all
  scope_builder.all
end
before(id) click to toggle source

Returns scope builder with filtering by ids older than provided @param [Integer] id @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 87
def self.before(id)
  scope_builder.before(id)
end
count() click to toggle source

Counts all objects @return [Integer]

# File lib/syncano/active_record/base.rb, line 28
def self.count
  scope_builder.count
end
create(attributes) click to toggle source

Creates new object with specified attributes @param [Hash] attributes @return [Object]

# File lib/syncano/active_record/base.rb, line 71
def self.create(attributes)
  new_object = self.new(attributes)
  new_object.save
  new_object
end
filterable_attributes() click to toggle source

Returns hash with filterable attributes @return [HashWithIndifferentAccess]

# File lib/syncano/active_record/base.rb, line 115
def self.filterable_attributes
  self._filterable_attributes ||= HashWithIndifferentAccess.new
end
find(id) click to toggle source

Returns one object found by id @param [Integer] id @return [Object]

# File lib/syncano/active_record/base.rb, line 64
def self.find(id)
  scope_builder.find(id)
end
first(amount = nil) click to toggle source

Returns first object or collection of first x objects @param [Integer] amount @return [Object, Array]

# File lib/syncano/active_record/base.rb, line 35
def self.first(amount = nil)
  scope_builder.first(amount)
end
folder() click to toggle source

Returns corresponding Syncano folder @return [Syncano::Resources::Folder]

# File lib/syncano/active_record/base.rb, line 93
def self.folder
  begin
    folder = collection.folders.find_by_name(folder_name)
  rescue Syncano::ApiError => e
    if e.message.starts_with?('DoesNotExist')
      folder = collection.folders.create(name: folder_name)
    else
      raise e
    end
  end
  folder
end
last(amount = nil) click to toggle source

Returns last object or collection of last x objects @param [Integer] amount @return [Object, Array]

# File lib/syncano/active_record/base.rb, line 42
def self.last(amount = nil)
  scope_builder.last(amount)
end
limit(amount) click to toggle source

Returns scope builder with limit parameter set to parameter @param [Integer] amount @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 109
def self.limit(amount)
  scope_builder.limit(amount)
end
map_from_syncano_attributes(attributes = {}) click to toggle source

Maps syncano attributes to corresponding model attributes @param [Hash] attributes @return [HashWithIndifferentAccess]

# File lib/syncano/active_record/base.rb, line 128
def self.map_from_syncano_attributes(attributes = {})
  mappings = HashWithIndifferentAccess.new(filterable_attributes.invert)
  HashWithIndifferentAccess[attributes.map {|k, v| [mappings[k] || k, v] }]
end
map_to_syncano_attribute(attribute) click to toggle source

Maps one model attribute to corresponding syncano attribute @param [Symbol, String] attribute @return [String]

# File lib/syncano/active_record/base.rb, line 144
def self.map_to_syncano_attribute(attribute)
  mappings = filterable_attributes
  mappings[attribute] || attribute
end
map_to_syncano_attributes(attributes = {}) click to toggle source

Maps model attributes to corresponding syncano attributes @param [Hash] attributes @return [HashWithIndifferentAccess]

# File lib/syncano/active_record/base.rb, line 136
def self.map_to_syncano_attributes(attributes = {})
  mappings = filterable_attributes
  HashWithIndifferentAccess[attributes.map {|k, v| [mappings[k] || k, v] }]
end
new(params = {}) click to toggle source

Constructor for model @param [Hash] params

Calls superclass method ActiveAttr::Dirty::new
# File lib/syncano/active_record/base.rb, line 151
def initialize(params = {})
  if params.is_a?(Syncano::Resources::DataObject)
    super(self.class.map_from_syncano_attributes(params.attributes).merge(id: params.id))
  else
    params.delete(:id)
    super(self.class.map_from_syncano_attributes(params))
  end
end
order(order) click to toggle source

Returns scope builder with order passed as first argument @param [String] order @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 57
def self.order(order)
  scope_builder.order(order)
end
scopes() click to toggle source

Returns hash with scopes @return [HashWithIndifferentAccess]

# File lib/syncano/active_record/base.rb, line 121
def self.scopes
  self._scopes ||= HashWithIndifferentAccess.new
end
since(id) click to toggle source

Returns scope builder with filtering by ids newer than provided @param [Integer] id @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 80
def self.since(id)
  scope_builder.since(id)
end
where(condition, *params) click to toggle source

Returns scope builder with condition passed as arguments @param [String] condition @param [Array] params @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 50
def self.where(condition, *params)
  scope_builder.where(condition, *params)
end

Private Class Methods

attribute(name, options = {}) click to toggle source

Defines model attribute @param [Symbol] name @param [Hash] options

Calls superclass method
# File lib/syncano/active_record/base.rb, line 285
def self.attribute(name, options = {})
  if options[:filterable].present?
    self.filterable_attributes = HashWithIndifferentAccess.new if filterable_attributes.nil?
    filterable_attributes[name] = options.delete(:filterable)
  end
  super(name, options)
end
collection() click to toggle source

Returns Syncano collection for storing Syncano::ActiveRecord objects @return [Syncano::Resource::Collection]

# File lib/syncano/active_record/base.rb, line 256
def self.collection
  SYNCANO_ACTIVERECORD_COLLECTION
end
filterable_attributes=(hash) click to toggle source

Setter for filterable_attributes attribute

# File lib/syncano/active_record/base.rb, line 267
def self.filterable_attributes=(hash)
  self._filterable_attributes = hash
end
folder_name() click to toggle source

Returns Syncano collection for storing Syncano::ActiveRecord objects @return [Syncano::Resource::Collection]

# File lib/syncano/active_record/base.rb, line 262
def self.folder_name
  name.pluralize
end
method_missing(name, *args) click to toggle source

Overwritten method_missing for handling calling defined scopes @param [String] name @param [Array] args

Calls superclass method
# File lib/syncano/active_record/base.rb, line 303
def self.method_missing(name, *args)
  if scopes[name].nil?
    super
  else
    scope_builder.send(name.to_sym, *args)
  end
end
scope(name, procedure) click to toggle source

Defines model scope @param [Symbol] name @param [Proc] procedure

# File lib/syncano/active_record/base.rb, line 296
def self.scope(name, procedure)
  scopes[name] = procedure
end
scope_builder() click to toggle source

Returns scope builder for current model @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 278
def self.scope_builder
  Syncano::ActiveRecord::ScopeBuilder.new(self)
end
scopes=(hash) click to toggle source

Setter for scopes attribute

# File lib/syncano/active_record/base.rb, line 272
def self.scopes=(hash)
  self._scopes = hash
end

Public Instance Methods

==(object) click to toggle source

Overwritten equality operator @param [Object] object @return [TrueClass, FalseClass]

# File lib/syncano/active_record/base.rb, line 163
def ==(object)
  self.class == object.class && self.id == object.id
end
destroy() click to toggle source

Deletes object from Syncano @return [TrueClass, FalseClass]

# File lib/syncano/active_record/base.rb, line 230
def destroy
  process_callbacks(:before_destroy)
  data_object = self.class.folder.data_objects.find(id)
  data_object.destroy
  process_callbacks(:after_destroy) if data_object.destroyed
  data_object.destroyed
end
new_record?() click to toggle source

Checks if object has not been saved in Syncano yet @return [TrueClass, FalseClass]

# File lib/syncano/active_record/base.rb, line 240
def new_record?
  !persisted?
end
persisted?() click to toggle source

Checks if object has been already saved in Syncano @return [TrueClass, FalseClass]

# File lib/syncano/active_record/base.rb, line 246
def persisted?
  id.present?
end
save() click to toggle source

Saves object in Syncano @return [TrueClass, FalseClass]

Calls superclass method ActiveAttr::Dirty#save
# File lib/syncano/active_record/base.rb, line 185
def save
  saved = false

  if valid?
    process_callbacks(:before_save)
    process_callbacks(persisted? ? :before_update : :before_create)

    data_object = persisted? ? self.class.folder.data_objects.find(id) : self.class.folder.data_objects.new
    data_object.attributes = self.class.map_to_syncano_attributes(attributes.except(:id, :created_at, :updated_at))
    data_object.save

    if data_object.saved?
      self.updated_at = data_object[:updated_at]

      if persisted?
        process_callbacks(:after_update)
      else
        self.id = data_object.id
        self.created_at = data_object[:created_at]
        process_callbacks(:after_create)
      end

      self.class.associations.values.select{ |association| association.belongs_to? }.each do |association|
        change = changes[association.foreign_key]

        if change.present?
          if change.last.nil? || association.associated_model.find(change.last).present?
            data_object.remove_parent(change.first) unless change.first.nil?
            data_object.add_parent(change.last) unless change.last.nil?
          end
        end
      end

      super

      process_callbacks(:after_save)
      saved = true
    end
  end

  saved
end
update_attributes(attributes) click to toggle source

Updates object with specified attributes @param [Hash] attributes @return [TrueClass, FalseClass]

# File lib/syncano/active_record/base.rb, line 170
def update_attributes(attributes)
  self.attributes = attributes
  self.save
end
valid?() click to toggle source

Performs validations @return [TrueClass, FalseClass]

Calls superclass method
# File lib/syncano/active_record/base.rb, line 177
def valid?
  process_callbacks(:before_validation)
  process_callbacks(:after_validation) if result = super
  result
end

Private Instance Methods

scope_builder(object_class) click to toggle source

Returns scope builder for specified class @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/base.rb, line 313
def scope_builder(object_class)
  Syncano::ActiveRecord::ScopeBuilder.new(object_class)
end