Module: Bhf::Mongoid::Document::ClassMethods

Defined in:
lib/bhf/mongoid/document.rb

Instance Method Summary collapse

Instance Method Details

#bhf_default_search(search_params) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/bhf/mongoid/document.rb', line 95

def bhf_default_search(search_params)
  return if (search_term = search_params[:text]).blank?
  
  # TODO: add mongoid search
  return where(name: /^antp/i)
  #return where("this.nick == 'antpaw'")
end

#bhf_embedded?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/bhf/mongoid/document.rb', line 137

def bhf_embedded?
  embedded?
end

#bhf_find_embed(parent_id, ref_id) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/bhf/mongoid/document.rb', line 126

def bhf_find_embed(parent_id, ref_id)
  get_embedded_parent parent_id do |parent, meta|
    relation = parent.send(meta.inverse_of)
    if parent.relations[meta.inverse_of.to_s].macro == :embeds_one
      relation
    else
      relation.find(ref_id)
    end
  end
end

#bhf_new_embed(parent_id, params = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/bhf/mongoid/document.rb', line 116

def bhf_new_embed(parent_id, params = nil)
  get_embedded_parent parent_id do |parent, meta|
    if parent.relations[meta.inverse_of.to_s].macro == :embeds_one
      parent.send("build_#{meta.inverse_of}", params)
    else
      parent.send(meta.inverse_of).build(params)
    end
  end
end

#bhf_primary_keyObject



77
78
79
80
# File 'lib/bhf/mongoid/document.rb', line 77

def bhf_primary_key
  # TODO: still needed?
  '_id'
end

#columns_hashObject



58
59
60
61
62
63
64
65
66
# File 'lib/bhf/mongoid/document.rb', line 58

def columns_hash
  c = {}
  fields.each_pair do |key, meta|
    next if meta.options[:metadata]
    next if key == '_type'
    c[key] = Field.new(meta)
  end
  c
end

#except(key) ⇒ Object



82
83
84
85
86
87
# File 'lib/bhf/mongoid/document.rb', line 82

def except(key)
  if key == :order || key == :sort
    #order_by.extras(sort: []) #TODO: drop default_scope criteria
  end
  self
end

#get_embedded_parent(parent_id, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bhf/mongoid/document.rb', line 103

def get_embedded_parent(parent_id, &block)
  relations.each do |key, meta|
    next unless meta.macro == :embedded_in
    parent = meta.class_name.constantize
    parent = parent.find(parent_id) rescue nil
    
    if parent
      return parent unless block_given?
      return block.call(parent, meta)
    end
  end
end

#order(a) ⇒ Object



89
90
91
92
93
# File 'lib/bhf/mongoid/document.rb', line 89

def order(a)
  field, direction = a.split(' ')
  return self if field.blank? or direction.blank?
  self.send(direction.downcase, field)
end

#reflectionsObject



68
69
70
71
72
73
74
75
# File 'lib/bhf/mongoid/document.rb', line 68

def reflections
  c = {}
  relations.each do |key, meta|
    next if meta.macro == :embedded_in
    c[key.to_sym] = Reflection.new(meta)
  end
  c
end