module ElocalApiSupport::ModelFromParams

Protected Instance Methods

allowed_filter_columns() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 75
def allowed_filter_columns
  associated_model_columns
end
allowed_sort_columns() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 71
def allowed_sort_columns
  associated_model_columns
end
associated_model() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 79
def associated_model
  @associated_model ||= associated_model_name.camelize.constantize
end
associated_model_columns() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 83
def associated_model_columns
  @associated_model_columns ||= associated_model.columns.map(&:name)
end
current_page() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 67
def current_page
  @current_page ||= (params[:page] || 1).to_i
end
default_per_page() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 59
def default_per_page
  500
end
filter_sort_col() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 87
def filter_sort_col
  params[:sort][:key] \
    if params[:sort] && params[:sort][:key] && allowed_sort_columns.include?(params[:sort][:key])
end
filter_sort_direction() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 92
def filter_sort_direction
  if params[:sort] && params[:sort][:direction] && %w(asc desc).include?(params[:sort][:direction])
    params[:sort][:direction]
  else
    ''
  end
end
filtered_objects() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 14
def filtered_objects
  @filtered_objects ||= with_pagination(with_sorting(with_filters(with_includes(associated_model))))
end
lookup_object() click to toggle source

Including classes need to define methods

- params
- associated_model_name
# File lib/elocal_api_support/model_from_params.rb, line 10
def lookup_object
  @lookup_object ||= with_includes(associated_model.where(id: params[:id])).first
end
per_page() click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 63
def per_page
  @per_page ||= (params[:per_page] || default_per_page).to_i
end
with_filters(rel) click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 26
def with_filters(rel)
  allowed_filter_columns.each do |param_name|
    if params[param_name].present?
      if respond_to?(:"with_#{associated_model.to_s.downcase}_by_#{param_name}", true)
        rel = send(:"with_#{associated_model.to_s.downcase}_by_#{param_name}", params[param_name])
      elsif associated_model.respond_to?(:"with_#{param_name}", true)
        rel = rel.send(:"with_#{param_name}", params[param_name])
      else
        rel = rel.where(param_name.to_sym => params[param_name])
      end
    end
  end

  rel
end
with_includes(rel) click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 55
def with_includes(rel)
  rel
end
with_pagination(rel) click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 18
def with_pagination(rel)
  if paginated_request?
    rel.page(current_page).per(per_page)
  else
    rel.page(current_page).limit(false)
  end
end
with_sorting(rel) click to toggle source
# File lib/elocal_api_support/model_from_params.rb, line 42
def with_sorting(rel)
  if filter_sort_col.present?
    if respond_to?(:"order_#{associated_model.to_s.downcase}_by_#{filter_sort_col}", true)
      rel = send(:"order_#{associated_model.to_s.downcase}_by_#{filter_sort_col}", rel)
    elsif associated_model.respond_to?(:"order_by_#{filter_sort_col}", true)
      rel = rel.send(:"order_by_#{filter_sort_col}", params[filter_sort_col], filter_sort_direction)
    else
      rel = rel.order(Arel.sql("#{associated_model.table_name}.#{filter_sort_col} #{filter_sort_direction}"))
    end
  end
  rel
end