Module: Bhf::Mongoid::Document::ClassMethods
- Defined in:
- lib/bhf/mongoid/document.rb
Instance Method Summary collapse
- #bhf_default_search(search_params) ⇒ Object
- #bhf_embedded? ⇒ Boolean
- #bhf_find_embed(parent_id, ref_id) ⇒ Object
- #bhf_new_embed(parent_id, params = nil) ⇒ Object
- #bhf_primary_key ⇒ Object
- #columns_hash ⇒ Object
- #except(key) ⇒ Object
- #get_embedded_parent(parent_id, &block) ⇒ Object
- #order(a) ⇒ Object
- #reflections ⇒ Object
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
131 132 133 |
# File 'lib/bhf/mongoid/document.rb', line 131 def 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 (parent_id, ref_id) parent_id do |parent, | relation = parent.send(.inverse_of) if parent.relations[.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 (parent_id, params = nil) parent_id do |parent, | if parent.relations[.inverse_of.to_s].macro == :embeds_one parent.send("build_#{.inverse_of}", params) else parent.send(.inverse_of).build(params) end end end |
#bhf_primary_key ⇒ Object
72 73 74 |
# File 'lib/bhf/mongoid/document.rb', line 72 def bhf_primary_key '_id' end |
#columns_hash ⇒ Object
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, | next if .[:metadata] next if key == '_type' c[key] = Field.new() 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 (parent_id, &block) relations.each do |key, | next unless .macro == :embedded_in parent = .class_name.constantize parent = parent.find(parent_id) rescue nil if parent return parent unless block_given? return block.call(parent, ) 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 |
#reflections ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/bhf/mongoid/document.rb', line 63 def reflections c = {} relations.each do |key, | next if .macro == :embedded_in c[key.to_sym] = Reflection.new() end c end |