module Lolita::Adapter::CommonHelper
Public Instance Methods
association_by_klass(given_klass)
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 121 def association_by_klass(given_klass) associations.select{|name,association| association.klass == given_klass }.values.first end
associations_class_names()
click to toggle source
Return all association class names
# File lib/lolita/adapter/common_helper.rb, line 115 def associations_class_names self.associations.map{|name,association| association.class_name } end
by_id(id)
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 138 def by_id(id) klass.where(klass.primary_key => id) end
filter(attributes={})
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 127 def filter attributes={} klass.where(attributes.reject{|_,v| v.nil? || v.to_s == "" }) end
find_by_id(id)
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 142 def find_by_id(id) self.klass.unscoped.merge(by_id(id)).first end
paginate(page, per, options = {})
click to toggle source
This method is used to paginate, main reason is for list and for index action. Method accepts three arguments page
- page that should be shown (integer) per
- how many records there should be in page options
- Hash with optional information. By default, Lolita::Configuration::List
passes request, with current request information. Also it passes :pagination_method that is used to detect if there is special method(-s) in model that should be used for creating page.
# File lib/lolita/adapter/common_helper.rb, line 154 def paginate(page, per, options = {}) pagination_builder = PaginationBuilder.new(self, page, per, options) pagination_builder.create_page end
record(orm_record)
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 110 def record(orm_record) Record.new(self,orm_record) end
reflect_on_association(name)
click to toggle source
Detect if class reflect on association by name
# File lib/lolita/adapter/common_helper.rb, line 132 def reflect_on_association(name) if orm_association = klass.reflect_on_association(name) self.class.const_get(:Association).new(orm_association,self) end end
set_state_for(record)
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 173 def set_state_for(record) unless record.respond_to?(:read_state!) class << record def set_state(new_state) @state_set = true @state = new_state end def have_state? @state_set end def read_state! set_state :read end def create_state! set_state :create end def update_state! set_state :update end def state set_state(:read) unless @state @state end def in_read_state? state == :read end def in_create_state? state == :create end def in_update_state? state == :update end end end record end
switch_record_state(record, state = nil)
click to toggle source
# File lib/lolita/adapter/common_helper.rb, line 159 def switch_record_state(record, state = nil) set_state_for(record) if state record.send(:"#{state}_state!") elsif !record.have_state? if record.new_record? record.create_state! else record.update_state! end end record end