Module: PopulateMe::DocumentMixins::AdminAdapter::ClassMethods
- Defined in:
- lib/populate_me/document_mixins/admin_adapter.rb
Instance Method Summary collapse
- #admin_distinct(field, o = {}) ⇒ Object
- #admin_find(o = {}) ⇒ Object
- #admin_find_first(o = {}) ⇒ Object
- #admin_get(id) ⇒ Object
- #admin_get_multiple(ids, o = {sort: nil}) ⇒ Object
- #admin_image_field ⇒ Object
- #sort_field_for(o = {}) ⇒ Object
- #to_admin_list(o = {}) ⇒ Object
Instance Method Details
#admin_distinct(field, o = {}) ⇒ Object
118 119 120 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 118 def admin_distinct field, o={} self.admin_find(o).map{|d| d.__send__ field}.compact.uniq end |
#admin_find(o = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 93 def admin_find o={} o[:query] ||= {} docs = self.cast do if o.key?( :fields ) documents.map do |h| h.select do |k,v| o[:fields].map(&:to_s).include?( k ) end end else documents end end.find_all do |d| o[:query].inject(true) do |out,(k,v)| out && (d.__send__(k)==v) end end docs.sort!(&@sort_proc) if @sort_proc.is_a?(Proc) docs end |
#admin_find_first(o = {}) ⇒ Object
114 115 116 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 114 def admin_find_first o={} self.admin_find(o)[0] end |
#admin_get(id) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 82 def admin_get id return self.admin_get_multiple(id) if id.is_a?(Array) self.cast do documents.find{|doc| doc[id_string_key] == id } end end |
#admin_get_multiple(ids, o = {sort: nil}) ⇒ Object
89 90 91 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 89 def admin_get_multiple ids, o={sort: nil} self.admin_find(o.merge(query: {id_string_key => {'$in' => ids.uniq.compact}})) end |
#admin_image_field ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 71 def admin_image_field res = self.fields.find do |k,v| if v[:type]==:attachment and !v[:variations].nil? v[:variations].any?{|var|var.name==:populate_me_thumb} else false end end res.nil? ? nil : res[0] end |
#sort_field_for(o = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 122 def sort_field_for o={} filter = o[:params][:filter] return nil if !filter.nil?&&filter.size>1 expected_scope = filter.nil? ? nil : filter.keys[0].to_sym f = self.fields.find do |k,v| v[:type]==:position&&v[:scope]==expected_scope end f.nil? ? nil : f[0] end |
#to_admin_list(o = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/populate_me/document_mixins/admin_adapter.rb', line 132 def to_admin_list o={} o[:params] ||= {} unless o[:params][:filter].nil? query = o[:params][:filter].inject({}) do |q, (k,v)| q[k.to_sym] = self.new.typecast(k,v) q end new_data = Rack::Utils.build_nested_query(data: o[:params][:filter]) end items = self.admin_find(query: query) { template: 'template_list', grid_view: self.settings[:grid_view]==true, page_title: self.to_s_short_plural, dasherized_class_name: WebUtils.dasherize_class_name(self.name), new_data: new_data, is_polymorphic: self.polymorphic?, polymorphic_type_values: self.polymorphic? ? self.fields[:polymorphic_type][:values] : nil, sort_field: self.sort_field_for(o), disable_plus: self.settings[:disable_plus]==true, disable_search: self.settings[:disable_search]==true, items: items.map do |d| d.to_admin_list_item(o) end } end |