Skip to content

Instantly share code, notes, and snippets.

@bellatrix988
Last active January 31, 2022 16:01
Show Gist options
  • Save bellatrix988/d1a4f80bc355fbb8b6976aaea092ca9a to your computer and use it in GitHub Desktop.
Save bellatrix988/d1a4f80bc355fbb8b6976aaea092ca9a to your computer and use it in GitHub Desktop.

DA-upgrade tips

Common Rails 3, 4 deprications

  1. replace after_filter and before_filter with after_action and before_action callbacks. after_filter and before_filter are both deprecated in rails 5

  2. Rails 5 stops some raw SQL and prevents SQL injections. For raw SQL there will be warning (error in rails 6) so we should change raw SQL string literals to Arel objects Arel.sql('')

  3. :condition no longer an acceptable key (replace with where) :order is no longer an option has_many :order (block) :uniq is no longer an option has_many (replace with -> { distinct })

  4. Rails 5: ActionController::Parameters Now Returns an Object Instead of a Hash Address the article undefined method `to_sym' for #<ActionController::Parameters:0x00005613f2464c20> To resolve try two ways:

# 1
def permitted_params
  params.permit!
end

# 2 
# replace with your model name
def customer_params
  params.require(:customer).permit(
  # list of params
  )
end
  1. error undefined method where_clauses

Ransack changes

meta_search gem deprecated and replaced with Ransack

search_methods method no longer exists. Purpose is to make the scope accessible by ransack search. Adding custom method > ransackable_scopes so that the scopes can be whitelisted and used. Also need to replace form_for with search_form_for

Refactor the sort_by_direction methods (ok for now but we can use ransack sort with custom ransackable_scopes

EX. REPLACE sort_by_directions :sold_units_repairs_complete, "inspection_completion_date is not null and intake_completion_date is not null"

WITH scope :sort_by_sold_units_repairs_complete_asc, lambda { order(Arel.sql("inspection_completion_date is not null and intake_completion_date is not null ASC")) } scope :sort_by_sold_units_repairs_complete_desc, lambda { order(Arel.sql("inspection_completion_date is not null and intake_completion_date is not null DESC")) }

  1. Replace
= hidden_field_tag 'search[meta_sort]', @search.meta_sort

with

= hidden_field_tag :s, value: params[:q].try(:[], :s)
  1. here is an example of the scope I mentioned...they will no longer be needed
scope :created_at_gteq, ->(date) { where("vehicles.created_at >= ?", date) }
scope :created_at_lteq, ->(date) { where("vehicles.created_at <= ?", date) }
  1. There is a new controller concern with name Ransackable. It turn on by adding a line in the controller you working on: inlcude Ransackable

Replace:

 # Replase this
 search = params[:search] || {}
 search[:meta_sort] ||= "stock.desc"
 # with this style
 search = q_param # use q_param method from the concern
 search[:s] ||= "stock desc" # replace meta_sort with s in ransack

Enumerize changes

  1. In Prod module may occur smth like undefined method _reflect_on_association.
belongs_to :production

replace with

belongs_to :production, class_name: '::Production'
  1. get_ _values was part of symbolize gem...for enumerize we no longer need get_ and _values is now .values UPD: probably even is now .options i.e.
Vehicle.get_inventory_type_values # Symbolize version <----- Replase this
Vehicle.inventory_type.value # Enumerize version. <----- with this
  1. uninitialized constant Production::EbayMotors
  # replace
  Production::EbayMotors
  # with
  Prod::Production::EbayMotors
  1. Replace methods with predicates for generate boolean methods by collection keys
# Symbolize version <----- Replase this
enumerize :status, :in => STATUSES.keys, :methods => true, :default => :in_progress, :allow_blank => true 
# Enumerize version. <----- with this
enumerize :status, in: STATUSES.keys, default: :in_progress, allow_blank: true,  predicates: true

SimpleForm tips

  1. error NotImplementedError (input should be implemented by classes inheriting from CollectionInput) can be resolved by adding a method in a class of input with issue.
def input
 input_html_options[:type] ||= input_type
end
  1. error No valid predicate for ftx_search ... in progress...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment