Module: Ditty::Helpers::Component

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

Instance Method Summary collapse

Instance Method Details

#apply_filter(ds, filter) ⇒ Object



92
93
94
95
96
97
# File 'lib/ditty/helpers/component.rb', line 92

def apply_filter(ds, filter)
  value = params[filter[:name]].send(filter[:modifier])
  return ds.where(filter[:field] => value) unless filter[:field].to_s.include? '.'

  ds.where(filter_field(filter) => filter_value(filter))
end

#check_countObject

param :count, Integer, min: 1, default: 10 # Can’t do this, since count can be ‘all`



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

def check_count
  return 10 if params[:count].nil?
  count = params[:count].to_i
  return count if count >= 1
  excp = Sinatra::Param::InvalidParameterError.new 'Parameter cannot be less than 1'
  excp.param = :count
  raise excp
end

#datasetObject



23
24
25
26
27
28
# File 'lib/ditty/helpers/component.rb', line 23

def dataset
  ds = policy_scope(settings.model_class)
  ds = ds.where Sequel.|(*search_filters) unless search_filters.blank?
  ds = ds.order ordering unless ordering.blank?
  filtered(ds)
end

#dehumanizedObject



59
60
61
# File 'lib/ditty/helpers/component.rb', line 59

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

#filter_association(filter) ⇒ Object



111
112
113
114
115
116
# File 'lib/ditty/helpers/component.rb', line 111

def filter_association(filter)
  assoc = filter[:field].to_s.split('.').first.to_sym
  assoc = settings.model_class.association_reflection(assoc)
  raise "Unknown association #{assoc}" if assoc.nil?
  assoc
end

#filter_field(filter) ⇒ Object



99
100
101
# File 'lib/ditty/helpers/component.rb', line 99

def filter_field(filter)
  filter[:field].to_s.split('.').first.to_sym
end

#filter_fieldsObject



63
64
65
# File 'lib/ditty/helpers/component.rb', line 63

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

#filter_value(filter) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/ditty/helpers/component.rb', line 103

def filter_value(filter)
  field = filter[:field].to_s.split('.').last.to_sym
  assoc = filter_association(filter)
  value = params[filter[:name].to_s].send(filter[:modifier])
  value = assoc.associated_dataset.first(field => value)
  value.nil? ? assoc.associated_class.new : value
end

#filtered(ds) ⇒ Object



71
72
73
74
75
76
# File 'lib/ditty/helpers/component.rb', line 71

def filtered(ds)
  filters.each do |filter|
    ds = apply_filter(ds, filter)
  end
  ds
end

#filtersObject



78
79
80
81
82
83
84
85
# File 'lib/ditty/helpers/component.rb', line 78

def filters
  filter_fields.map do |filter|
    next if params[filter[:name]].blank?
    filter[:field] ||= filter[:name]
    filter[:modifier] ||= :to_s # TODO: Do this with Sinatra Param?
    filter
  end.compact
end

#heading(action = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ditty/helpers/component.rb', line 47

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

#listObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ditty/helpers/component.rb', line 30

def list
  param :q, String
  param :page, Integer, min: 1, default: 1
  param :sort, String
  param :order, String, in: %w[asc desc], transform: :downcase, default: 'asc'
  # TODO: Can we dynamically validate the search / filter fields?

  ds = dataset
  ds = ds.dataset if ds.respond_to?(:dataset)
  return ds if params[:count] == 'all'
  params[:count] = check_count

  # Account for difference between sequel paginate and will paginate
  return ds.paginate(page: params[:page], per_page: params[:count]) if ds.is_a?(Array)
  ds.paginate(params[:page], params[:count])
end

#orderingObject



87
88
89
90
# File 'lib/ditty/helpers/component.rb', line 87

def ordering
  return if params[:sort].blank?
  Sequel.send(params[:order].to_sym, params[:sort].to_sym)
end

#search_filtersObject



118
119
120
121
# File 'lib/ditty/helpers/component.rb', line 118

def search_filters
  return [] if params[:q].blank?
  searchable_fields.map { |f| Sequel.ilike(f.to_sym, "%#{params[:q]}%") }
end

#searchable_fieldsObject



67
68
69
# File 'lib/ditty/helpers/component.rb', line 67

def searchable_fields
  self.class.const_defined?('SEARCHABLE') ? self.class::SEARCHABLE : []
end