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



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

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

#bhf_embedded?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/bhf/mongoid/document.rb', line 131

def bhf_embedded?
  embedded?
end

#bhf_find_embed(parent_id, ref_id) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/bhf/mongoid/document.rb', line 120

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



110
111
112
113
114
115
116
117
118
# File 'lib/bhf/mongoid/document.rb', line 110

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



72
73
74
# File 'lib/bhf/mongoid/document.rb', line 72

def bhf_primary_key
  '_id'
end

#columns_hashObject



53
54
55
56
57
58
59
60
61
# File 'lib/bhf/mongoid/document.rb', line 53

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



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

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bhf/mongoid/document.rb', line 97

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



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

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

#reflectionsObject



63
64
65
66
67
68
69
70
# File 'lib/bhf/mongoid/document.rb', line 63

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