How to use order method of Sort Package

Best Active_mocker_ruby code snippet using Sort.order

sort_helper.rb

Source:sort_helper.rb Github

copy

Full Screen

...5# Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.6# Jean-Philippe Lang, 20097# License: This source code is released under the MIT license.8#9# - Consecutive clicks toggle the column's sort order.10# - Sort state is maintained by a session hash entry.11# - CSS classes identify sort column and state.12# - Typically used in conjunction with the Pagination module.13#14# Example code snippets:15#16# Controller:17#18# helper :sort19# include SortHelper20#21# def list22# sort_init 'last_name'23# sort_update %w(first_name last_name)24# @items = Contact.find_all nil, sort_clause25# end26#27# Controller (using Pagination module):28#29# helper :sort30# include SortHelper31#32# def list33# sort_init 'last_name'34# sort_update %w(first_name last_name)35# @contact_pages, @items = paginate :contacts,36# :order_by => sort_clause,37# :per_page => 1038# end39#40# View (table header in list.rhtml):41#42# <thead>43# <tr>44# <%= sort_header_tag('id', :title => 'Sort by contact ID') %>45# <%= sort_header_tag('last_name', :caption => 'Name') %>46# <%= sort_header_tag('phone') %>47# <%= sort_header_tag('address', :width => 200) %>48# </tr>49# </thead>50#51# - Introduces instance variables: @sort_default, @sort_criteria52# - Introduces param :sort53#54module SortHelper55 class SortCriteria56 def initialize57 @criteria = []58 end59 def available_criteria=(criteria)60 unless criteria.is_a?(Hash)61 criteria = criteria.inject({}) {|h,k| h[k] = k; h}62 end63 @available_criteria = criteria64 end65 def from_param(param)66 @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}67 normalize!68 end69 def criteria=(arg)70 @criteria = arg71 normalize!72 end73 def to_param74 @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')75 end76 # Returns an array of SQL fragments used to sort the list77 def to_sql78 sql = @criteria.collect do |k,o|79 if s = @available_criteria[k]80 s = [s] unless s.is_a?(Array)81 s.collect {|c| append_order(c, o ? "ASC" : "DESC")}82 end83 end.flatten.compact84 sql.blank? ? nil : sql85 end86 def to_a87 @criteria.dup88 end89 def add!(key, asc)90 @criteria.delete_if {|k,o| k == key}91 @criteria = [[key, asc]] + @criteria92 normalize!93 end94 def add(*args)95 r = self.class.new.from_param(to_param)96 r.add!(*args)97 r98 end99 def first_key100 @criteria.first && @criteria.first.first101 end102 def first_asc?103 @criteria.first && @criteria.first.last104 end105 def empty?106 @criteria.empty?107 end108 private109 def normalize!110 @criteria ||= []111 @criteria = @criteria.collect {|s| s = Array(s); [s.first, (s.last == false || s.last == 'desc') ? false : true]}112 @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria113 @criteria.slice!(3)114 self115 end116 # Appends ASC/DESC to the sort criterion unless it has a fixed order117 def append_order(criterion, order)118 if criterion =~ / (asc|desc)$/i119 criterion120 else121 "#{criterion} #{order}"122 end123 end124 # Appends DESC to the sort criterion unless it has a fixed order125 def append_desc(criterion)126 append_order(criterion, "DESC")127 end128 end129 def sort_name130 controller_name + '_' + action_name + '_sort'131 end132 # Initializes the default sort.133 # Examples:134 #135 # sort_init 'name'136 # sort_init 'id', 'desc'137 # sort_init ['name', ['id', 'desc']]138 # sort_init [['name', 'desc'], ['id', 'desc']]139 #140 def sort_init(*args)141 case args.size142 when 1143 @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]]144 when 2145 @sort_default = [[args.first, args.last]]146 else147 raise ArgumentError148 end149 end150 # Updates the sort state. Call this in the controller prior to calling151 # sort_clause.152 # - criteria can be either an array or a hash of allowed keys153 #154 def sort_update(criteria, sort_name=nil)155 sort_name ||= self.sort_name156 @sort_criteria = SortCriteria.new157 @sort_criteria.available_criteria = criteria158 @sort_criteria.from_param(params[:sort] || session[sort_name])159 @sort_criteria.criteria = @sort_default if @sort_criteria.empty?160 session[sort_name] = @sort_criteria.to_param161 end162 # Clears the sort criteria session data163 #164 def sort_clear165 session[sort_name] = nil166 end167 # Returns an SQL sort clause corresponding to the current sort state.168 # Use this to sort the controller's table items collection.169 #170 def sort_clause()171 @sort_criteria.to_sql172 end173 def sort_criteria174 @sort_criteria175 end176 # Returns a link which sorts by the named column.177 #178 # - column is the name of an attribute in the sorted record collection.179 # - the optional caption explicitly specifies the displayed link text.180 # - 2 CSS classes reflect the state of the link: sort and asc or desc181 #182 def sort_link(column, caption, default_order)183 css, order = nil, default_order184 if column.to_s == @sort_criteria.first_key185 if @sort_criteria.first_asc?186 css = 'sort asc'187 order = 'desc'188 else189 css = 'sort desc'190 order = 'asc'191 end192 end193 caption = column.to_s.humanize unless caption194 sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }195 link_to(caption, {:params => request.query_parameters.merge(sort_options)}, :class => css)196 end197 # Returns a table header <th> tag with a sort link for the named column198 # attribute.199 #200 # Options:201 # :caption The displayed link name (defaults to titleized column name).202 # :title The tag's 'title' attribute (defaults to 'Sort by :caption').203 #204 # Other options hash entries generate additional table header tag attributes.205 #206 # Example:207 #208 # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>209 #210 def sort_header_tag(column, options = {})211 caption = options.delete(:caption) || column.to_s.humanize212 default_order = options.delete(:default_order) || 'asc'213 options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title]214 content_tag('th', sort_link(column, caption, default_order), options)215 end216 # Returns the css classes for the current sort order217 #218 # Example:219 #220 # sort_css_classes221 # # => "sort-by-created-on sort-desc"222 def sort_css_classes223 if @sort_criteria.first_key224 "sort-by-#{@sort_criteria.first_key.to_s.dasherize} sort-#{@sort_criteria.first_asc? ? 'asc' : 'desc'}"225 end226 end227end...

Full Screen

Full Screen

table_for.rb

Source:table_for.rb Github

copy

Full Screen

...52 end53 def build_table_header(col)54 classes = Arbre::HTML::ClassList.new55 sort_key = sortable? && col.sortable? && col.sort_key56 params = request.query_parameters.except :page, :order, :commit, :format57 classes << 'sortable' if sort_key58 classes << "sorted-#{current_sort[1]}" if sort_key && current_sort[0] == sort_key59 classes << col.html_class60 if sort_key61 th class: classes do62 link_to col.pretty_title, params: params, order: "#{sort_key}_#{order_for_sort_key(sort_key)}"63 end64 else65 th col.pretty_title, class: classes66 end67 end68 def build_table_body69 @tbody = tbody do70 # Build enough rows for our collection71 @collection.each do |elem|72 classes = [helpers.cycle('odd', 'even')]73 if @row_class74 classes << @row_class.call(elem)75 end76 tr(class: classes.flatten.join(' '), id: dom_id_for(elem))77 end78 end79 end80 def build_table_cell(col, resource)81 td class: col.html_class do82 html = helpers.format_attribute(resource, col.data)83 # Don't add the same Arbre twice, while still allowing format_attribute to call status_tag84 current_arbre_element << html unless current_arbre_element.children.include? html85 end86 end87 # Returns an array for the current sort order88 # current_sort[0] #=> sort_key89 # current_sort[1] #=> asc | desc90 def current_sort91 @current_sort ||= begin92 order_clause = active_admin_config.order_clause.new(active_admin_config, params[:order])93 if order_clause.valid?94 [order_clause.field, order_clause.order]95 else96 []97 end98 end99 end100 # Returns the order to use for a given sort key101 #102 # Default is to use 'desc'. If the current sort key is103 # 'desc' it will return 'asc'104 def order_for_sort_key(sort_key)105 current_key, current_order = current_sort106 return 'desc' unless current_key == sort_key107 current_order == 'desc' ? 'asc' : 'desc'108 end109 def default_options110 {111 i18n: @resource_class112 }113 end114 class Column115 attr_accessor :title, :data , :html_class116 def initialize(*args, &block)117 @options = args.extract_options!118 @title = args[0]119 html_classes = [:col]120 if @options.has_key?(:class)121 html_classes << @options.delete(:class)...

Full Screen

Full Screen

filterable.rb

Source:filterable.rb Github

copy

Full Screen

...6 # Include on an ApplicationRecord object via:7 # 'include Filterable'8 #9 # Filter and sort on parameters like:10 # 'ObjectName.filter_and_sort(params.slice(:example1), params.slice(:order, :order_dir))'11 #12 # Override #default_sort to modify the default sort order if no/invalid order params are supplied13 # Override #base_query to change the root query for the table, e.g. to include relations14 #15 # @param filtering_params The parameters to filter the table on16 # @param ordering_params The parameters to order the table on17 def filter_and_sort(filtering_params, ordering_params)18 results = base_query19 filtering_params.each do |key, value|20 if value.present?21 results = results.public_send("filter_by_#{key}", value)22 end23 end24 # filter field and direction to known values25 order_field = ordering_params[:order]26 order_direction = ordering_params[:order_dir]27 unless order_field.present? && order_direction.present?28 return default_sort(results)29 end30 return default_sort(results) unless (column_names + dynamic_fields).include?(order_field)31 unless %w[asc desc].include?(order_direction.downcase)32 return default_sort(results)33 end34 # check for overridden order scope35 if respond_to?("order_by_#{order_field}")36 return results.public_send("order_by_#{order_field}", order_direction)37 end38 results.order("#{ordering_params[:order]} #{ordering_params[:order_dir]}")39 end40 # define a default sort order on a query41 def default_sort(results)42 results43 end44 # define a base query for the type45 def base_query46 where(nil)47 end48 # define an empty array for dynamic fields49 def dynamic_fields50 []51 end52 end53end...

Full Screen

Full Screen

order

Using AI Code Generation

copy

Full Screen

1Sort.order(1,2,3)2 def self.order(*args)3 def self.order(*args)4Sort.order(1,2,3)5 def self.order(*args)6Sort.order(1,2,3)7 def self.order(*args)8Sort.order(1,2,3)9 def self.order(*args)10Sort.order(1,2,3)11 def self.order(*args)12Sort.order(1,2,3)13 def self.order(*args)

Full Screen

Full Screen

order

Using AI Code Generation

copy

Full Screen

1 def order(array)2 self.sort_by {|x| x}3 self.sort_by {|x| x}4 self.sort_by {|x| x}5 self.sort_by {|x| x}6 self.sort_by {|x| x}7 self.sort_by {|x| x}8 self.sort_by {|x| x}9 self.sort_by {|x| x}10 self.sort_by {|x| x}11 self.sort_by {|x| x}12 self.sort_by {|x|13Sort.order(1,2,3)14 def self.order(*args)15Sort.order(1,2,3)16 def self.order(*args)17Sort.order(1,2,3)18 def self.order(*args)

Full Screen

Full Screen

order

Using AI Code Generation

copy

Full Screen

1puts Sort.order(arr)2 def self.order(arr)3The require method is used to import a file that is in a different directory than the file that is importing it. The require method is used when the file that

Full Screen

Full Screen

order

Using AI Code Generation

copy

Full Screen

1 def order(array)2 self.sort_by {|x| x}3 self.sort_by {|x| x}4 self.sort_by {|x| x}5 self.sort_by {|x| x}6 self.sort_by {|x| x}7 self.sort_by {|x| x}8 self.sort_by {|x| x}9 self.sort_by {|x| x}10 self.sort_by {|x| x}11 self.sort_by {|x| x}12 self.sort_by {|x|

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful