class Syncano::ActiveRecord::Base
Class for integrating ActiveRecord
functionality
Public Class Methods
Gets collection with all objects @return [Array]
# File lib/syncano/active_record/base.rb, line 22 def self.all scope_builder.all end
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
Counts all objects @return [Integer]
# File lib/syncano/active_record/base.rb, line 28 def self.count scope_builder.count end
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
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
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
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
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
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
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
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
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
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
Constructor for model @param [Hash] params
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
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
Returns hash with scopes @return [HashWithIndifferentAccess]
# File lib/syncano/active_record/base.rb, line 121 def self.scopes self._scopes ||= HashWithIndifferentAccess.new end
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
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
Defines model attribute @param [Symbol] name @param [Hash] options
# 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
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
Setter for filterable_attributes
attribute
# File lib/syncano/active_record/base.rb, line 267 def self.filterable_attributes=(hash) self._filterable_attributes = hash end
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
Overwritten method_missing
for handling calling defined scopes @param [String] name @param [Array] args
# 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
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
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
Setter for scopes attribute
# File lib/syncano/active_record/base.rb, line 272 def self.scopes=(hash) self._scopes = hash end
Public Instance Methods
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
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
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
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
Saves object in Syncano
@return [TrueClass, FalseClass]
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
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
Performs validations @return [TrueClass, FalseClass]
# 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
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