Module: Ditty::Helpers::Component

Includes:
ActiveSupport::Inflector
Defined in:
lib/ditty/helpers/component.rb

Instance Method Summary collapse

Instance Method Details

#apply_filter(dataset, filter) ⇒ Object



55
56
57
58
59
60
# File 'lib/ditty/helpers/component.rb', line 55

def apply_filter(dataset, filter)
  value = params[filter[:name].to_s]
  return dataset if value == '' || value.nil?
  value = value.send(filter[:modifier]) if filter[:modifier]
  dataset.where(filter[:field].to_sym => value)
end

#base_pathObject



38
39
40
# File 'lib/ditty/helpers/component.rb', line 38

def base_path
  settings.base_path || "#{settings.map_path}/#{dasherize(view_location)}"
end

#datasetObject



11
12
13
# File 'lib/ditty/helpers/component.rb', line 11

def dataset
  filtered(policy_scope(settings.model_class))
end

#dehumanizedObject



34
35
36
# File 'lib/ditty/helpers/component.rb', line 34

def dehumanized
  settings.dehumanized || underscore(heading)
end

#filtered(dataset) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/ditty/helpers/component.rb', line 46

def filtered(dataset)
  filters.each do |filter|
    next unless params[filter[:name].to_s]
    filter[:field] ||= filter[:name]
    dataset = apply_filter(dataset, filter)
  end
  dataset
end

#filtersObject



42
43
44
# File 'lib/ditty/helpers/component.rb', line 42

def filters
  self.class.const_defined?('FILTERS') ? self.class::FILTERS : []
end

#heading(action = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ditty/helpers/component.rb', line 22

def heading(action = nil)
  @headings ||= begin
    heading = titleize(demodulize(settings.model_class))
    h = Hash.new(heading)
    h[:new] = "New #{heading}"
    h[:list] = pluralize heading
    h[:edit] = "Edit #{heading}"
    h
  end
  @headings[action]
end

#listObject



15
16
17
18
19
20
# File 'lib/ditty/helpers/component.rb', line 15

def list
  params['count'] ||= 10
  params['page'] ||= 1

  dataset.select.paginate(params['page'].to_i, params['count'].to_i)
end