Module: BcMiddleware::SearchApi
- Defined in:
- lib/bc-middleware/search_api.rb
Instance Method Summary collapse
- #attribute_fields ⇒ Object
- #collection ⇒ Object
- #extract_attributes(klass) ⇒ Object
- #page_term ⇒ Object
- #paginate_terms ⇒ Object
- #per_term ⇒ Object
- #relationship_fields ⇒ Object
- #resource_class_attributes ⇒ Object
- #search_terms ⇒ Object
Instance Method Details
#attribute_fields ⇒ Object
41 42 43 |
# File 'lib/bc-middleware/search_api.rb', line 41 def attribute_fields params[:fields].present? ? params[:fields] : resource_class_attributes end |
#collection ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/bc-middleware/search_api.rb', line 5 def collection get_collection_ivar || begin c = end_of_association_chain if respond_to?(:search_terms) && (terms = search_terms).present? c = c.search(terms) end if paginate_terms.present? && page_term.present? && per_term.present? if c.respond_to?(:scoped) c = c.search.page(page_term).per(per_term) else c = c.page(page_term).per(per_term) end end set_collection_ivar(c.respond_to?(:scoped) ? c.scoped : c.all) end end |
#extract_attributes(klass) ⇒ Object
72 73 74 |
# File 'lib/bc-middleware/search_api.rb', line 72 def extract_attributes(klass) klass.accessible_attributes.select(&:present?) + [klass.primary_key] end |
#page_term ⇒ Object
33 34 35 |
# File 'lib/bc-middleware/search_api.rb', line 33 def page_term paginate_terms[:page] end |
#paginate_terms ⇒ Object
29 30 31 |
# File 'lib/bc-middleware/search_api.rb', line 29 def paginate_terms params[:paginate] end |
#per_term ⇒ Object
37 38 39 |
# File 'lib/bc-middleware/search_api.rb', line 37 def per_term paginate_terms[:per] end |
#relationship_fields ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bc-middleware/search_api.rb', line 45 def relationship_fields @relationship_fields ||= begin include_params = params[:include].present? ? params[:include] : {} raise(ArgumentError.new("wrong format for include parameter")) unless include_params.is_a?(Hash) result = {} include_params.each do |(relationship_name, relationship_value)| # TODO verificar as relacoes if relationship_value.present? result[relationship_name] = relationship_value else relationship = resource_class.reflect_on_all_associations.detect {|r| r.name == relationship_name.to_sym} if relationship.nil? raise(InvalidRelationship.new("Relationship with name #{relationship_name} not found for #{resource_class}")) end result[relationship_name] = extract_attributes(relationship.klass) end end result end end |
#resource_class_attributes ⇒ Object
68 69 70 |
# File 'lib/bc-middleware/search_api.rb', line 68 def resource_class_attributes @resource_class_attributes ||= extract_attributes(resource_class) end |
#search_terms ⇒ Object
25 26 27 |
# File 'lib/bc-middleware/search_api.rb', line 25 def search_terms params[:search] end |