Class: Syncano::ActiveRecord::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::Dirty, ActiveAttr::Model, ActiveModel::ForbiddenAttributesProtection, Associations, Callbacks
Defined in:
lib/syncano/active_record/base.rb

Overview

Class for integrating ActiveRecord functionality

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#process_callbacks

Constructor Details

#initialize(params = {}) ⇒ Base

Constructor for model

Parameters:

  • params (Hash) (defaults to: {})


151
152
153
154
155
156
157
158
# 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

Class Method Details

.allArray

Gets collection with all objects

Returns:

  • (Array)


22
23
24
# File 'lib/syncano/active_record/base.rb', line 22

def self.all
  scope_builder.all
end

.before(id) ⇒ Syncano::ActiveRecord::ScopeBuilder

Returns scope builder with filtering by ids older than provided

Parameters:

  • id (Integer)

Returns:



87
88
89
# File 'lib/syncano/active_record/base.rb', line 87

def self.before(id)
  scope_builder.before(id)
end

.countInteger

Counts all objects

Returns:

  • (Integer)


28
29
30
# File 'lib/syncano/active_record/base.rb', line 28

def self.count
  scope_builder.count
end

.create(attributes) ⇒ Object

Creates new object with specified attributes

Parameters:

  • attributes (Hash)

Returns:

  • (Object)


71
72
73
74
75
# 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_attributesHashWithIndifferentAccess

Returns hash with filterable attributes

Returns:

  • (HashWithIndifferentAccess)


115
116
117
# File 'lib/syncano/active_record/base.rb', line 115

def self.filterable_attributes
  self._filterable_attributes ||= HashWithIndifferentAccess.new
end

.find(id) ⇒ Object

Returns one object found by id

Parameters:

  • id (Integer)

Returns:

  • (Object)


64
65
66
# File 'lib/syncano/active_record/base.rb', line 64

def self.find(id)
  scope_builder.find(id)
end

.first(amount = nil) ⇒ Object, Array

Returns first object or collection of first x objects

Parameters:

  • amount (Integer) (defaults to: nil)

Returns:

  • (Object, Array)


35
36
37
# File 'lib/syncano/active_record/base.rb', line 35

def self.first(amount = nil)
  scope_builder.first(amount)
end

.folderSyncano::Resources::Folder

Returns corresponding Syncano folder



93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object, Array

Returns last object or collection of last x objects

Parameters:

  • amount (Integer) (defaults to: nil)

Returns:

  • (Object, Array)


42
43
44
# File 'lib/syncano/active_record/base.rb', line 42

def self.last(amount = nil)
  scope_builder.last(amount)
end

.limit(amount) ⇒ Syncano::ActiveRecord::ScopeBuilder

Returns scope builder with limit parameter set to parameter

Parameters:

  • amount (Integer)

Returns:



109
110
111
# File 'lib/syncano/active_record/base.rb', line 109

def self.limit(amount)
  scope_builder.limit(amount)
end

.map_from_syncano_attributes(attributes = {}) ⇒ HashWithIndifferentAccess

Maps syncano attributes to corresponding model attributes

Parameters:

  • attributes (Hash) (defaults to: {})

Returns:

  • (HashWithIndifferentAccess)


128
129
130
131
# 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) ⇒ String

Maps one model attribute to corresponding syncano attribute

Parameters:

  • attribute (Symbol, String)

Returns:

  • (String)


144
145
146
147
# 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 = {}) ⇒ HashWithIndifferentAccess

Maps model attributes to corresponding syncano attributes

Parameters:

  • attributes (Hash) (defaults to: {})

Returns:

  • (HashWithIndifferentAccess)


136
137
138
139
# 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

.order(order) ⇒ Syncano::ActiveRecord::ScopeBuilder

Returns scope builder with order passed as first argument

Parameters:

  • order (String)

Returns:



57
58
59
# File 'lib/syncano/active_record/base.rb', line 57

def self.order(order)
  scope_builder.order(order)
end

.scopesHashWithIndifferentAccess

Returns hash with scopes

Returns:

  • (HashWithIndifferentAccess)


121
122
123
# File 'lib/syncano/active_record/base.rb', line 121

def self.scopes
  self._scopes ||= HashWithIndifferentAccess.new
end

.since(id) ⇒ Syncano::ActiveRecord::ScopeBuilder

Returns scope builder with filtering by ids newer than provided

Parameters:

  • id (Integer)

Returns:



80
81
82
# File 'lib/syncano/active_record/base.rb', line 80

def self.since(id)
  scope_builder.since(id)
end

.where(condition, *params) ⇒ Syncano::ActiveRecord::ScopeBuilder

Returns scope builder with condition passed as arguments

Parameters:

  • condition (String)
  • params (Array)

Returns:



50
51
52
# File 'lib/syncano/active_record/base.rb', line 50

def self.where(condition, *params)
  scope_builder.where(condition, *params)
end

Instance Method Details

#==(object) ⇒ TrueClass, FalseClass

Overwritten equality operator

Parameters:

  • object (Object)

Returns:

  • (TrueClass, FalseClass)


163
164
165
# File 'lib/syncano/active_record/base.rb', line 163

def ==(object)
  self.class == object.class && self.id == object.id
end

#destroyTrueClass, FalseClass

Deletes object from Syncano

Returns:

  • (TrueClass, FalseClass)


230
231
232
233
234
235
236
# 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?TrueClass, FalseClass

Checks if object has not been saved in Syncano yet

Returns:

  • (TrueClass, FalseClass)


240
241
242
# File 'lib/syncano/active_record/base.rb', line 240

def new_record?
  !persisted?
end

#persisted?TrueClass, FalseClass

Checks if object has been already saved in Syncano

Returns:

  • (TrueClass, FalseClass)


246
247
248
# File 'lib/syncano/active_record/base.rb', line 246

def persisted?
  id.present?
end

#saveTrueClass, FalseClass

Saves object in Syncano

Returns:

  • (TrueClass, FalseClass)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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) ⇒ TrueClass, FalseClass

Updates object with specified attributes

Parameters:

  • attributes (Hash)

Returns:

  • (TrueClass, FalseClass)


170
171
172
173
# File 'lib/syncano/active_record/base.rb', line 170

def update_attributes(attributes)
  self.attributes = attributes
  self.save
end

#valid?TrueClass, FalseClass

Performs validations

Returns:

  • (TrueClass, FalseClass)


177
178
179
180
181
# File 'lib/syncano/active_record/base.rb', line 177

def valid?
  process_callbacks(:before_validation)
  process_callbacks(:after_validation) if result = super
  result
end