Module: Lolita::Adapter::CommonHelper

Included in:
ActiveRecord, Mongoid
Defined in:
lib/lolita/adapter/common_helper.rb

Defined Under Namespace

Classes: PaginationBuilder, Record

Instance Method Summary collapse

Instance Method Details

#association_by_klass(given_klass) ⇒ Object



121
122
123
124
125
# 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_namesObject

Return all association class names



115
116
117
118
119
# 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) ⇒ Object



138
139
140
# File 'lib/lolita/adapter/common_helper.rb', line 138

def by_id(id)
  klass.where(klass.primary_key => id)
end

#filter(attributes = {}) ⇒ Object



127
128
129
# 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) ⇒ Object



142
143
144
# 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 = {}) ⇒ Object

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.



154
155
156
157
# 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) ⇒ Object



110
111
112
# File 'lib/lolita/adapter/common_helper.rb', line 110

def record(orm_record)
  Record.new(self,orm_record)
end

#reflect_on_association(name) ⇒ Object

Detect if class reflect on association by name



132
133
134
135
136
# 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) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
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
# 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) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# 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