# File lib/findable/base.rb, line 22 def primary_key "id" end
class Findable::Base
Public Class Methods
all()
click to toggle source
# File lib/findable/base.rb, line 26 def all collection!(query.all) end
arel_table()
click to toggle source
# File lib/findable/base.rb, line 13 def arel_table raise NotActiveRecord.new(self) end
create(attrs = {})
click to toggle source
# File lib/findable/base.rb, line 91 def create(attrs = {}) record = new(attrs) record.save record end
Also aliased as: create!
exists?(obj)
click to toggle source
# File lib/findable/base.rb, line 111 def exists?(obj) if _id = id_from(obj) query.exists?(_id) else false end end
find(ids)
click to toggle source
# File lib/findable/base.rb, line 30 def find(ids) if records = find_by_ids(ids).presence ids.is_a?(Array) ? collection!(records) : records.first else raise not_found(id: ids) end end
find_by(conditions)
click to toggle source
# File lib/findable/base.rb, line 38 def find_by(conditions) if conditions.is_a?(Hash) conditions.symbolize_keys! if index = conditions.keys.detect {|key| key.in?(indexes) } value = conditions.delete(index) if index == :id records = find_by_ids(value) else records = find_by_index(index, value) end case when records.empty? then nil when conditions.empty? then records.first else records.detect {|record| conditions.all? {|k, v| record.public_send(k) == v } } end else all.find_by(conditions.dup) end else find_by_ids(conditions).first end end
find_by!(conditions)
click to toggle source
# File lib/findable/base.rb, line 65 def find_by!(conditions) find_by(conditions.dup) || (raise not_found(conditions)) end
new(params = {})
click to toggle source
# File lib/findable/base.rb, line 137 def initialize(params = {}) params = params.with_indifferent_access params.keys.each {|attr| self.class.define_field(attr) } @_attributes = params end
ordered_find(*_ids)
click to toggle source
Extension
# File lib/findable/base.rb, line 100 def ordered_find(*_ids) _ids.flatten! find(_ids).ordered_find(_ids) end
primary_key()
click to toggle source
query()
click to toggle source
# File lib/findable/base.rb, line 119 def query @_query ||= Query.new(self) end
where(conditions)
click to toggle source
# File lib/findable/base.rb, line 69 def where(conditions) conditions.symbolize_keys! if index = conditions.keys.detect {|key| key.in?(indexes) } value = conditions.delete(index) if index == :id records = find_by_ids(value) else records = find_by_index(index, value) end if conditions.empty? collection!(records) else collection!(records.select {|record| conditions.all? {|k, v| record.public_send(k) == v } }) end else all.where(conditions.dup) end end
Private Class Methods
collection!(records)
click to toggle source
# File lib/findable/base.rb, line 124 def collection!(records) records.is_a?(Array) ? Collection.new(self, records) : records end
id_from(obj)
click to toggle source
# File lib/findable/base.rb, line 132 def id_from(obj) obj.is_a?(self) ? obj.id : obj.to_i end
not_found(params)
click to toggle source
# File lib/findable/base.rb, line 128 def not_found(params) RecordNotFound.new(self, params) end
Public Instance Methods
attributes()
click to toggle source
# File lib/findable/base.rb, line 173 def attributes @_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end
delete()
click to toggle source
# File lib/findable/base.rb, line 168 def delete self.class.delete(self) end
Also aliased as: destroy
hash()
click to toggle source
# File lib/findable/base.rb, line 151 def hash id.hash end
id()
click to toggle source
# File lib/findable/base.rb, line 143 def id attributes[:id].presence end
id=(value)
click to toggle source
# File lib/findable/base.rb, line 147 def id=(value) attributes[:id] = value end
new_record?()
click to toggle source
# File lib/findable/base.rb, line 155 def new_record? id ? !self.class.exists?(self) : true end
persisted?()
click to toggle source
# File lib/findable/base.rb, line 159 def persisted? !new_record? end
save()
click to toggle source
# File lib/findable/base.rb, line 163 def save self.class.insert(self) end
Also aliased as: save!