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
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
137 138 139 |
# File 'lib/bhf/mongoid/document.rb', line 137 def 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 (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
116 117 118 119 120 121 122 123 124 |
# File 'lib/bhf/mongoid/document.rb', line 116 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
77 78 79 80 |
# File 'lib/bhf/mongoid/document.rb', line 77 def bhf_primary_key # TODO: still needed? '_id' end |
#columns_hash ⇒ Object
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, | next if .[:metadata] next if key == '_type' c[key] = Field.new() 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 (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
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 |
#reflections ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/bhf/mongoid/document.rb', line 68 def reflections c = {} relations.each do |key, | next if .macro == :embedded_in c[key.to_sym] = Reflection.new() end c end |