Module: Shamu::Entities::ActiveRecord

Defined in:
lib/shamu/entities/active_record.rb

Overview

Mixins for working with ActiveRecord resources as entities.

module Domain
  module Models
    class Account < ActiveRecord::Base
      extend Shamu::Entities::ActiveRecord
    end
  end

  class AccountListScope < Shamu::Entities::ListScope
    attribute :name
  end
end

list_scope = Domain::AccountListScope.new( name: "Flipper" )
records = Domain::Models::Account.all.by_list_scope( list_scope )

Instance Method Summary collapse

Instance Method Details

#apply_sort(criteria, field, direction) ⇒ ActiveRecord::Relation

Apply sorting to the criteria for the given field and the given direction.

Parameters:

  • criteria (ActiveRecord::Relation)

    to sort.

  • field (Symbol)

    to sort by.

  • direction (Symbol)

    to sort.

Returns:

  • (ActiveRecord::Relation)

    the sorted criteria.



54
55
56
57
58
59
60
# File 'lib/shamu/entities/active_record.rb', line 54

def apply_sort( criteria, field, direction )
  if attribute_method?( field )
    criteria.order( arel_table[ field ].send( direction ) )
  else
    criteria
  end
end

#by_list_scope(scope) ⇒ ActiveRecord::Relation

Apply the filters defined in a ListScope to an ActiveRecord::Relation.

Parameters:

Returns:

  • (ActiveRecord::Relation)


28
29
30
31
32
33
34
35
36
37
# File 'lib/shamu/entities/active_record.rb', line 28

def by_list_scope( scope )
  criteria = all
  criteria = apply_paging_scope( criteria, scope )        if scope.respond_to?( :paged? )
  criteria = apply_scoped_paging_scope( criteria, scope ) if scope.respond_to?( :scoped_page? )
  criteria = apply_window_paging_scope( criteria, scope ) if scope.respond_to?( :window_paged? )
  criteria = apply_dates_scope( criteria, scope )         if scope.respond_to?( :dated? )
  criteria = apply_sorting_scope( criteria, scope )       if scope.respond_to?( :sorted? )
  criteria = apply_custom_list_scope( criteria, scope )
  criteria
end