Class: Category

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ErpTechSvcs::Utils::DefaultNestedSetMethods
Defined in:
app/models/category.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_filters(filters, statement = nil) ⇒ ActiveRecord::Relation

Filter records

Parameters:

  • filters (Hash)

    a hash of filters to be applied,

  • statement (ActiveRecord::Relation) (defaults to: nil)

    the query being built

Returns:

  • (ActiveRecord::Relation)

    the query being built



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/category.rb', line 48

def apply_filters(filters, statement=nil)
  unless statement
    statement = self
  end

  # filter by parent
  if filters[:parent]
    if filters[:parent].is_integer?
      statement = statement.where(categories: {parent_id: filters[:parent]})
    else
      statement = statement.where(categories: {parent_id: Category.iid(filters[:parent])})
    end
  end

  # filter by query which will filter on description
  if filters[:query]
    statement = statement.where('description like ?', "%#{filters[:query].strip}%")
  end

  statement
end

.iid(internal_identifier) ⇒ Object



107
108
109
# File 'app/models/category.rb', line 107

def iid(internal_identifier)
  where("internal_identifier = ?", internal_identifier).first
end

.scope_by_dba_organization(dba_organization) ⇒ ActiveRecord::Relation Also known as: scope_by_dba

scope by dba organization

Parameters:

  • dba_organization (Party)

    dba organization to scope by

Returns:

  • (ActiveRecord::Relation)


75
76
77
# File 'app/models/category.rb', line 75

def scope_by_dba_organization(dba_organization)
  scope_by_party(dba_organization, {role_types: 'dba_org'})
end

.scope_by_party(party, options = {}) ⇒ ActiveRecord::Relation

scope by party

or an array of Party ids comma separated or an Array

Parameters:

  • party (Integer | Party | Array)

    either a id of Party record, a Party record, an array of Party records

  • options (Hash) (defaults to: {})

    options to apply to this scope

Options Hash (options):

  • :role_types (String | Array)

    BizTxnAcctPtyRtype internal identifiers to include in the scope,

Returns:

  • (ActiveRecord::Relation)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/category.rb', line 90

def scope_by_party(party, options={})
  statement = joins(:entity_party_roles)
                  .where(entity_party_roles: {party_id: party}).uniq

  if options[:role_types]
    role_types = options[:role_types]
    unless role_types.is_a? Array
      role_types = role_types.split(',')
    end

    statement = statement.joins(entity_party_roles: :role_type)
                    .where(role_types: {internal_identifier: role_types})
  end

  statement
end

Instance Method Details

#to_data_hashObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/category.rb', line 112

def to_data_hash
  to_hash(
      only: [
          :id,
          :description,
          :internal_identifier,
          :created_at,
          :updated_at
      ],
      leaf: leaf?
  )
end