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



119
120
121
122
123
# File 'lib/lolita/adapter/common_helper.rb', line 119

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



113
114
115
116
117
# File 'lib/lolita/adapter/common_helper.rb', line 113

def associations_class_names
  self.associations.map{|name,association|
    association.class_name
  }
end

#by_id(id) ⇒ Object



136
137
138
# File 'lib/lolita/adapter/common_helper.rb', line 136

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

#filter(attributes = {}) ⇒ Object



125
126
127
# File 'lib/lolita/adapter/common_helper.rb', line 125

def filter attributes={}
  klass.where(attributes.reject{|k,v| v.blank? })
end

#find_by_id(id) ⇒ Object



140
141
142
# File 'lib/lolita/adapter/common_helper.rb', line 140

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.



152
153
154
155
# File 'lib/lolita/adapter/common_helper.rb', line 152

def paginate(page, per, options = {})
  pagination_builder = PaginationBuilder.new(self, page, per, options)
  pagination_builder.create_page
end

#record(orm_record) ⇒ Object



108
109
110
# File 'lib/lolita/adapter/common_helper.rb', line 108

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

#reflect_on_association(name) ⇒ Object

Detect if class reflect on association by name



130
131
132
133
134
# File 'lib/lolita/adapter/common_helper.rb', line 130

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



171
172
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
# File 'lib/lolita/adapter/common_helper.rb', line 171

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/lolita/adapter/common_helper.rb', line 157

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