Sha256: 7f149720054db8e151fc144a4a4e2cab1392040335831e8212d501fd482ce7c0
Contents?: true
Size: 1.92 KB
Versions: 6
Compression:
Stored size: 1.92 KB
Contents
# http://ricostacruz.com/cheatsheets/sequel.html # http://sequel.jeremyevans.net/rdoc/files/doc/model_plugins_rdoc.html class Sequel::Model module ClassMethods def find_by what where(what).first end # active record like define scope # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/ClassMethods.html def scope name, body=nil, &block block ||= body dataset_module{define_method(name, &block)} end # instance scope, same as scope but runs on instance # iscope(:notes) { Note.where(created_by:id) } def iscope name, body=nil, &block block ||= body define_method(name, &block) end def first_or_new filter where(filter).first || new(filter) end def first_or_create filter where(filter).first || create(filter) end end module InstanceMethods def cache_key "#{self.class}/#{id}" end def attributes ret = {} for el in columns ret[el.to_s] = send(el.to_s) rescue '-' end ret end def touch self[:updated_at] = Time.now.utc save columns: [:updated_at] end def to_h ret = {} for el in self.keys ret[el] = send el end ret end def creator self[:created_by] ? User.find(self[:created_by]) : nil end def parent_model model_type.constantize.find(model_id) end # has?(:name, "Name is not defined") -> errors.add("Name is not defined") # has?(:name) -> false def has?(*args) if args[1] && args[1].kind_of?(String) unless self[args[0]].present? errors.add(args[0], args[1]) return false end return true end args.each { |el| return false unless self[el].present? } true end def unique?(field) select(field).xwhere('id<>?', id).count == 0 end def save! save end end module DatasetMethods end end
Version data entries
6 entries across 6 versions & 1 rubygems