Module: RestingPug::Params

Defined in:
lib/resting_pug/params.rb

Overview

Describes transformations with params

Used in:

Instance Method Summary collapse

Instance Method Details

#default_sort_paramsObject (protected)

Note:

Override permitted_fields_for_sort to set allowed fields to sort.

Returns a hash with default sorting params

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a default_sort_params to set default sorting to sorted descending by title
  def default_sort_params
    { title: :desc }
  end
end

See Also:

Used in:


237
238
239
# File 'lib/resting_pug/params.rb', line 237

def default_sort_params
  {id: :desc}
end

#filter_paramsObject (protected)

Note:

Returns strong parameters which are used for filtering subjects


191
192
193
# File 'lib/resting_pug/params.rb', line 191

def filter_params
  params[:filter] ? params.require(:filter).permit(permitted_fields_for_filter_with_arrays) : nil
end

#pagination_paramsObject (protected)

Note:

Override per_page_range to set minimum and maximum per_page param. Override per_page_default to set default per_page param.

Handles pagination params and return a hash like { page: 1, per_page: 10 }


249
250
251
252
253
# File 'lib/resting_pug/params.rb', line 249

def pagination_params
  page = params[:page].try(:to_i) || 1
  per_page = [per_page_range[:min], params[:per_page].try(:to_i) || per_page_default, per_page_range[:max]].sort[1]
  return { page: page, per_page: per_page }
end

#params_for_createObject (protected)

Note:

Override permitted_fields_for_create to set allowed fields. Override subject_model_sym to change the root attrubute of params.

Returns strong parameters which are used in creating action


169
170
171
# File 'lib/resting_pug/params.rb', line 169

def params_for_create
  params.require(subject_model_sym).permit(permitted_fields_for_create)
end

#params_for_updateObject (protected)

Note:

Override permitted_fields_for_update to set allowed fields. Override subject_model_sym to change the root attrubute of params.

Returns strong parameters which are used in updating action


180
181
182
# File 'lib/resting_pug/params.rb', line 180

def params_for_update
  params.require(subject_model_sym).permit(permitted_fields_for_update)
end

#per_page_defaultObject (protected)

Note:

Override per_page_range to set minimum and maximum per_page param

Returns a default per_page param

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a per_page_default to set default page size to 15
  def per_page_default
    15
  end
end

Used in:


285
286
287
# File 'lib/resting_pug/params.rb', line 285

def per_page_default
  10
end

#per_page_rangeObject (protected)

Note:

Override per_page_default to set default per_page param.

Returns a hash minimum and maximum per_page param like { page: 1, per_page: 10 }

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a per_page_range to set minimum possible value for per_page 10 and maximum 20
  def per_page_range
    { min: 10, max: 20 }
  end
end

Used in:


268
269
270
# File 'lib/resting_pug/params.rb', line 268

def per_page_range
  { min: 5, max: 100 }
end

#permitted_fieldsArray (protected)

Note:

Override this method to add or remove fields that you don't want API to use.

Returns an array with all fields that API can use

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields method to use only :id and :title
  def permitted_fields
    [:id, :title]
  end
end

Used in:

Use:


28
29
30
# File 'lib/resting_pug/params.rb', line 28

def permitted_fields
  subject_model.column_names.map(&:to_sym)
end

#permitted_fields_for_createArray (protected)

Note:

Override this method to add or remove fields that you don't want API to use for creating.

Returns an array with all fields that API will allow to use while creating a subject

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields_for_create method to use every permitted attribute except :title
  def permitted_fields_for_create
    permitted_fields - [:title]
  end
end

Used in:

Use:


67
68
69
# File 'lib/resting_pug/params.rb', line 67

def permitted_fields_for_create
  permitted_fields - [:id, :created_at, :updated_at]
end

#permitted_fields_for_filterArray (protected)

Note:

Override this method to add or remove fields that you don't want API to use for filtering by single value. If you want to change attributes that can be filtered by arrays of values check #permitted_fields_for_filter_arrays

Returns an array with all fields that API will allow to use while filtering subjects by single value

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields_for_sort method to use every permitted attribute except :title
  def permitted_fields_for_sort
    permitted_fields - [:title]
  end
end

Used in:

Use:


126
127
128
# File 'lib/resting_pug/params.rb', line 126

def permitted_fields_for_filter
  permitted_fields - [:created_at, :updated_at]
end

#permitted_fields_for_filter_arraysArray (protected)

Note:

Override this method to add or remove fields that you don't want API to use for filtering by array of values. If you want to change attributes that can be filtered by single value check #permitted_fields_for_filter

Returns an array with all fields that API will allow to use while filtering subjects by array of values

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields_for_filter_arrays method to use only :id and :title
  def permitted_fields_for_filter_arrays
    { id: [], title: []}
  end
end

Used in:

Use:


146
147
148
# File 'lib/resting_pug/params.rb', line 146

def permitted_fields_for_filter_arrays
  permitted_fields_for_filter.map { |field| { field => [] } }
end

#permitted_fields_for_filter_with_arraysArray (protected)

Note:

To set fields that can be used check #permitted_fields_for_filter and #permitted_fields_for_filter_arrays

Returns an array with all fields that API will allow to use while filtering subjects both by single values and by array of values


156
157
158
# File 'lib/resting_pug/params.rb', line 156

def permitted_fields_for_filter_with_arrays
  permitted_fields_for_filter + permitted_fields_for_filter_arrays
end

#permitted_fields_for_showArray (protected)

Note:

Override this method to add or remove fields that you don't want API to show.

Returns an array with all fields that API will show

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields_for_show method to show every permitted attribute except :title
  def permitted_fields_for_show
    permitted_fields - [:title]
  end
end

Used in:

Use:


48
49
50
# File 'lib/resting_pug/params.rb', line 48

def permitted_fields_for_show
  permitted_fields
end

#permitted_fields_for_sortArray (protected)

Note:

Override this method to add or remove fields that you don't want API to use for sorting.

Returns an array with all fields that API will allow to use while sorting subjects

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields_for_sort method to use every permitted attribute except :title
  def permitted_fields_for_sort
    permitted_fields - [:title]
  end
end

Used in:

Use:


105
106
107
# File 'lib/resting_pug/params.rb', line 105

def permitted_fields_for_sort
  permitted_fields
end

#permitted_fields_for_updateArray (protected)

Note:

Override this method to add or remove fields that you don't want API to use for updating.

Returns an array with all fields that API will allow to use while updating a subject

Examples:

class BooksController < ApplicationController
  include RestingPug::Base

  private
  # Override a permitted_fields_for_update method to use every permitted attribute except :title
  def permitted_fields_for_update
    permitted_fields - [:title]
  end
end

Used in:

Use:


86
87
88
# File 'lib/resting_pug/params.rb', line 86

def permitted_fields_for_update
  permitted_fields - [:id, :created_at, :updated_at]
end

#sort_paramsObject (protected)

Note:

Override permitted_fields_for_sort to set allowed fields to sort. Override default_sort_params to set default sort params.

Transforms a string like “-id,name” to a hash { id: :desc, name: :asc }


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/resting_pug/params.rb', line 203

def sort_params
  sort_hash = nil
  if params[:sort]
    sort_array = params[:sort].split(',')
    sort_array = sort_array.map do |field|
      is_desc = field.split('-').count > 1
      name = field.split('-').last
      { name: name.to_sym, is_desc: is_desc }
    end
    sort_array.select! do |field|
      permitted_fields_for_sort.include? field[:name]
    end
    sort_array.each do |field|
      sort_hash ||= {}
      sort_hash[field[:name]] = field[:is_desc] ? :desc : :asc
    end
  end
  sort_hash || default_sort_params
end