Module: ColumnSort::ColumnSortInModel::ClassMethods
- Defined in:
- lib/column_sort/column_sort_in_model.rb
Instance Method Summary collapse
-
#column_sort(params) ⇒ Object
Calls a sorting scope based on the given parameters.
-
#sortable_columns(columns) ⇒ Object
Set the different types of sorts the front end can call.
Instance Method Details
#column_sort(params) ⇒ Object
Calls a sorting scope based on the given parameters
If the column and direction parameters are set, attempt to find a matching scope and call it. Otherwise, returns a chainable object that does not affect the outcome of the query.
See the sortable_columns method for more information on how this works.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/column_sort/column_sort_in_model.rb', line 43 def column_sort(params) # if parameters are present, attempt to run a scope, otherwise add an empty where if params.present? && params[:column].present? && params[:direction].present? # if sortable_column_list is empty, just run the scope # otherwise, check if the parameters match the values in sortable_column_list if sortable_column_list.nil? scope_name = "column_sort_#{params[:column]}_#{params[:direction]}".downcase send(scope_name) else # if sortable_column_list is not empty, check to make sure the column is a member of the list # before continuing if !sortable_column_list.include?(params[:column].to_sym) || params[:direction].nil? where({}) else scope_name = "column_sort_#{params[:column]}_#{params[:direction]}".downcase send(scope_name) end end else where({}) end end |
#sortable_columns(columns) ⇒ Object
Set the different types of sorts the front end can call
This method is used to ensure that whatever sort is called from the column_sort scope will not trigger a missing method exception. This will throw exceptions for the missing scopes when the application. Additionally, sorts that are not included in this list will not be executed when column_sort is called. Taking strings from a params hash and directly converting them to method names without any security is a little scary.
This method is optional. If it is not called, column_sort will blindly attempt to call any scope given to it.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/column_sort/column_sort_in_model.rb', line 22 def sortable_columns(columns) columns = [columns] unless columns.is_a? Array mattr_accessor :sortable_column_list self.sortable_column_list = columns columns.each do |column| if !self.respond_to?("column_sort_#{column}_asc") || !self.respond_to?("column_sort_#{column}_desc") throw "Missing sort scope for #{column}" end end end |