Module: FacetFor

Defined in:
lib/facet_for.rb,
lib/facet_for/version.rb

Defined Under Namespace

Classes: Facet

Constant Summary collapse

PREDICATES =
[
 ['Contains', :cont],
 ['Doesn\'t Contain', :not_cont],
 ['Starts With', :start],
 ['Doesn\'t Start With', :not_start],
 ['Ends With', :end],
 ['Doesn\'t End With', :not_end],
 ['Between', :between],
 ['Is Null', :null],
 ['Is Not Null', :not_null],
 ['Collection', :collection],
 ['Is True', :true],
 ['Is False', :false]
]
VERSION =
"0.1.6"

Class Method Summary collapse

Class Method Details

.column_options(q) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/facet_for.rb', line 36

def self.column_options(q)
  column_options = []
  column_options.push([q.klass.to_s, options_for_model(q.klass)])

  q.context.searchable_associations.each do |association|
    association_model = association.camelcase.singularize.constantize
    column_options.push([association_model.to_s,
                         options_for_model(association_model, association)])
  end

  column_options
end

.create_facet(*args) ⇒ Object

We can use reflections to determine has_many and belongs_to associations



28
29
30
31
32
33
# File 'lib/facet_for.rb', line 28

def self.create_facet(*args)
  options = args.extract_options!

  @facet = Facet.new(options)
  @facet.render_facet.html_safe
end

.field_options(q) ⇒ Object



49
50
51
52
53
# File 'lib/facet_for.rb', line 49

def self.field_options(q)
  field_options = options_for_model(q.klass) | q.context.searchable_associations.map { |a| [a.titleize, a.to_sym] }

  field_options
end

.options_for_model(model, association = nil, grouped = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/facet_for.rb', line 55

def self.options_for_model(model, association = nil, grouped = false)
  options = []
  preface = ''

  if association # preface field names for cross model displaying
    preface = "#{association}."
  end

  if model.ransackable_attributes
    options = model.ransackable_attributes.map { |a| [a.titleize, "#{preface}#{a}".to_sym] }
  end

  options
end

.predicatesObject



22
23
24
# File 'lib/facet_for.rb', line 22

def self.predicates
  PREDICATES
end