48 lines
977 B
Ruby
48 lines
977 B
Ruby
|
module ViewModels
|
||
|
class RecipeCriteria
|
||
|
|
||
|
SORT_COLUMNS = :created_at, :name, :rating, :total_time
|
||
|
|
||
|
attr_writer :sort_column, :sort_direction
|
||
|
attr_writer :page, :per
|
||
|
|
||
|
def initialize(params = {})
|
||
|
params ||= {}
|
||
|
([:sort_column, :sort_direction, :page, :per]).each do |attr|
|
||
|
setter = "#{attr}="
|
||
|
if params[attr]
|
||
|
self.send(setter, params[attr])
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def sort_column
|
||
|
@sort_column ||= SORT_COLUMNS.first
|
||
|
@sort_column = @sort_column.to_sym
|
||
|
unless SORT_COLUMNS.include? @sort_column
|
||
|
@sort_column = SORT_COLUMNS.first
|
||
|
end
|
||
|
|
||
|
@sort_column
|
||
|
end
|
||
|
|
||
|
def sort_direction
|
||
|
@sort_direction ||= :asc
|
||
|
@sort_direction = @sort_direction.to_sym
|
||
|
unless [:asc, :desc].include? @sort_direction
|
||
|
@sort_direction = :asc
|
||
|
end
|
||
|
|
||
|
@sort_direction
|
||
|
end
|
||
|
|
||
|
def page
|
||
|
@page.to_i || 1
|
||
|
end
|
||
|
|
||
|
def per
|
||
|
@per.to_i || 50
|
||
|
end
|
||
|
|
||
|
end
|
||
|
end
|