Class: Syncano::ActiveRecord::Base
- Inherits:
-
Object
- Object
- Syncano::ActiveRecord::Base
- 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
-
.all ⇒ Array
Gets collection with all objects.
-
.before(id) ⇒ Syncano::ActiveRecord::ScopeBuilder
Returns scope builder with filtering by ids older than provided.
-
.count ⇒ Integer
Counts all objects.
-
.create(attributes) ⇒ Object
Creates new object with specified attributes.
-
.filterable_attributes ⇒ HashWithIndifferentAccess
Returns hash with filterable attributes.
-
.find(id) ⇒ Object
Returns one object found by id.
-
.first(amount = nil) ⇒ Object, Array
Returns first object or collection of first x objects.
-
.folder ⇒ Syncano::Resources::Folder
Returns corresponding Syncano folder.
-
.last(amount = nil) ⇒ Object, Array
Returns last object or collection of last x objects.
-
.limit(amount) ⇒ Syncano::ActiveRecord::ScopeBuilder
Returns scope builder with limit parameter set to parameter.
-
.map_from_syncano_attributes(attributes = {}) ⇒ HashWithIndifferentAccess
Maps syncano attributes to corresponding model attributes.
-
.map_to_syncano_attribute(attribute) ⇒ String
Maps one model attribute to corresponding syncano attribute.
-
.map_to_syncano_attributes(attributes = {}) ⇒ HashWithIndifferentAccess
Maps model attributes to corresponding syncano attributes.
-
.order(order) ⇒ Syncano::ActiveRecord::ScopeBuilder
Returns scope builder with order passed as first argument.
-
.scopes ⇒ HashWithIndifferentAccess
Returns hash with scopes.
-
.since(id) ⇒ Syncano::ActiveRecord::ScopeBuilder
Returns scope builder with filtering by ids newer than provided.
-
.where(condition, *params) ⇒ Syncano::ActiveRecord::ScopeBuilder
Returns scope builder with condition passed as arguments.
Instance Method Summary collapse
-
#==(object) ⇒ TrueClass, FalseClass
Overwritten equality operator.
-
#destroy ⇒ TrueClass, FalseClass
Deletes object from Syncano.
-
#initialize(params = {}) ⇒ Base
constructor
Constructor for model.
-
#new_record? ⇒ TrueClass, FalseClass
Checks if object has not been saved in Syncano yet.
-
#persisted? ⇒ TrueClass, FalseClass
Checks if object has been already saved in Syncano.
-
#save ⇒ TrueClass, FalseClass
Saves object in Syncano.
-
#update_attributes(attributes) ⇒ TrueClass, FalseClass
Updates object with specified attributes.
-
#valid? ⇒ TrueClass, FalseClass
Performs validations.
Methods included from Callbacks
Constructor Details
#initialize(params = {}) ⇒ Base
Constructor for model
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
.all ⇒ Array
Gets collection with all objects
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
87 88 89 |
# File 'lib/syncano/active_record/base.rb', line 87 def self.before(id) scope_builder.before(id) end |
.count ⇒ Integer
Counts all objects
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
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_attributes ⇒ HashWithIndifferentAccess
Returns hash with filterable attributes
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
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
35 36 37 |
# File 'lib/syncano/active_record/base.rb', line 35 def self.first(amount = nil) scope_builder.first(amount) end |
.folder ⇒ Syncano::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..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
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
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
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
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
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
57 58 59 |
# File 'lib/syncano/active_record/base.rb', line 57 def self.order(order) scope_builder.order(order) end |
.scopes ⇒ HashWithIndifferentAccess
Returns hash with scopes
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
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
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
163 164 165 |
# File 'lib/syncano/active_record/base.rb', line 163 def ==(object) self.class == object.class && self.id == object.id end |
#destroy ⇒ TrueClass, FalseClass
Deletes object from Syncano
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
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
246 247 248 |
# File 'lib/syncano/active_record/base.rb', line 246 def persisted? id.present? end |
#save ⇒ TrueClass, FalseClass
Saves object in Syncano
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
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
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 |