Module: Carnival::ModelHelper::ClassMethods

Defined in:
app/models/carnival/model_helper.rb

Instance Method Summary collapse

Instance Method Details

#get_elements_for_select(params = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/carnival/model_helper.rb', line 37

def get_elements_for_select(params = {})
  return all if params[:carnival_scope].nil?
  scopes = params[:carnival_scope][:scopes]
  return all if scopes.empty?
  model_object = params[:carnival_scope][:model_object]
  conditions = {}
  scopes.each do |scp|
    value = model_object.send scp
    conditions[scp] = value if !value.nil?
  end
  return where(conditions) if !conditions.empty?
  []
end

#list_for_checkboxObject



31
32
33
34
35
# File 'app/models/carnival/model_helper.rb', line 31

def list_for_checkbox
  list = []
  all.each {|object| list << [object.id, object.to_label] }
  list
end

#list_for_searchObject



25
26
27
28
29
# File 'app/models/carnival/model_helper.rb', line 25

def list_for_search
  select = []
  select << ['', '']
  select.concat all.collect{|c|[c.to_label, c.to_label]}
end

#list_for_select(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/carnival/model_helper.rb', line 12

def list_for_select(params = {})
  select = []
  select << ['', ''] if params[:add_empty_option]
  query = all
  query = query.where(params[:query]) if params[:query].present?
  if params[:reverse]
    select.concat query.collect{|c|[c.to_label, c.id]}
  else
    select.concat query.collect{|c|[c.id, c.to_label]}
  end
  select
end