Class: Polytag::TagGroup

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/polytag/tag_group.rb

Defined Under Namespace

Modules: Owner

Class Method Summary collapse

Class Method Details

.find_by_hash(hash = {}) ⇒ Object



48
49
50
# File 'lib/polytag/tag_group.rb', line 48

def find_by_hash(hash = {})
  search_by_hash(hash).first
end

.search_by_hash(hash = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/polytag/tag_group.rb', line 24

def search_by_hash(hash = {})
  return self if hash.empty?
  conditions = {}

  # Query by owner information
  if hash[:owner]
    conditions.merge!(owner_type: "#{hash[:owner].class}")
    conditions.merge!(owner_id: hash[:owner].id)
  else
    if hash[:owner_type]
      conditions.merge!(owner_type: "#{hash[:owner_type]}")
      conditions.merge!(owner_id: hash[:owner_id]) if hash[:owner_id]
    end
  end

  # Query by tag group name
  conditions.merge!(name: "#{hash[:name]}") if hash[:name]

  # Query by tag group id
  conditions.merge!(id: hash[:id]) if hash[:id]

  where(conditions)
end