Module: Bhf::Mongoid::Document::ClassMethods
- Defined in:
- lib/bhf/mongoid/document.rb
Instance Method Summary collapse
- #bhf_attribute_method?(column_name) ⇒ Boolean
- #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
- #get_embedded_parent(parent_id, &block) ⇒ Object
- #order(a) ⇒ Object
- #reflections ⇒ Object
- #reorder(a) ⇒ Object
Instance Method Details
#bhf_attribute_method?(column_name) ⇒ Boolean
76 77 78 |
# File 'lib/bhf/mongoid/document.rb', line 76 def bhf_attribute_method?(column_name) attribute_method?(column_name) end |
#bhf_default_search(search_params) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bhf/mongoid/document.rb', line 92 def bhf_default_search(search_params) return self if (search_term = search_params['text']).blank? arr = [] columns_hash.each_pair do |column, props| is_number = search_term.to_i.to_s == search_term || search_term.to_f.to_s == search_term if props.type == :primary_key arr << {props.name.to_sym => search_term} elsif props.type == :string || props.type == :text arr << {props.name.to_sym => /#{search_term}/i} elsif props.type == :integer && is_number arr << {props.name.to_sym => search_term.to_i} elsif props.type == :float && is_number arr << {props.name.to_sym => search_term.to_f} end end self.or(arr) end |
#bhf_embedded? ⇒ Boolean
157 158 159 |
# File 'lib/bhf/mongoid/document.rb', line 157 def end |
#bhf_find_embed(parent_id, ref_id) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/bhf/mongoid/document.rb', line 141 def (parent_id, ref_id) parent_id do |parent, | key_name = if .inverse_of? .inverse_of else .inverse_foreign_key.pluralize end.to_s relation = parent.send(key_name) if parent.relations[key_name].macro == :embeds_one relation else relation.find(ref_id) end end end |
#bhf_new_embed(parent_id, params = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/bhf/mongoid/document.rb', line 126 def (parent_id, params = nil) parent_id do |parent, | key_name = if .inverse_of? .inverse_of else .inverse_foreign_key.pluralize end.to_s if parent.relations[key_name] and parent.relations[key_name].macro == :embeds_one parent.send("build_#{key_name}", params) else parent.send(key_name).build(params) end end end |
#bhf_primary_key ⇒ Object
80 81 82 |
# File 'lib/bhf/mongoid/document.rb', line 80 def bhf_primary_key '_id' end |
#columns_hash ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/bhf/mongoid/document.rb', line 57 def columns_hash c = {} fields.each_pair do |key, | next if .[:metadata] next if key == '_type' c[key] = Field.new() end c end |
#get_embedded_parent(parent_id, &block) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bhf/mongoid/document.rb', line 113 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 block.call(parent, ) if block_given? return parent end end end |
#order(a) ⇒ Object
84 85 86 |
# File 'lib/bhf/mongoid/document.rb', line 84 def order(a) order_by(a) end |
#reflections ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/bhf/mongoid/document.rb', line 67 def reflections c = {} relations.each do |key, | next if .macro == :embedded_in c[key] = Reflection.new() end c end |
#reorder(a) ⇒ Object
88 89 90 |
# File 'lib/bhf/mongoid/document.rb', line 88 def reorder(a) order_by(a) end |