Skip to content

Commit

Permalink
Merge pull request #1360 from tvdeyen/rails-5-2-deprecations
Browse files Browse the repository at this point in the history
Get rid of Rails 5.2 deprecations
  • Loading branch information
tvdeyen authored Feb 26, 2018
2 parents 4927a21 + 16348bc commit 7c8a5b2
Show file tree
Hide file tree
Showing 24 changed files with 96 additions and 39 deletions.
7 changes: 0 additions & 7 deletions app/models/alchemy.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/alchemy/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#

module Alchemy
class Attachment < ActiveRecord::Base
class Attachment < BaseRecord
include Alchemy::Filetypes
include Alchemy::NameConversions
include Alchemy::Touching
Expand Down
13 changes: 13 additions & 0 deletions app/models/alchemy/base_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Alchemy
def self.table_name_prefix
'alchemy_'
end

class BaseRecord < ActiveRecord::Base
self.abstract_class = true

def active_record_5_1?
ActiveRecord.gem_version >= Gem::Version.new('5.1.0')
end
end
end
2 changes: 1 addition & 1 deletion app/models/alchemy/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Views for cells are inside the +app/views/cells+ folder in your project.
#
module Alchemy
class Cell < ActiveRecord::Base
class Cell < BaseRecord
include Alchemy::Logger

belongs_to :page, required: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#

module Alchemy
class Content < ActiveRecord::Base
class Content < BaseRecord
include Alchemy::Logger
include Alchemy::Touching
include Alchemy::Hints
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#

module Alchemy
class Element < ActiveRecord::Base
class Element < BaseRecord
include Alchemy::Logger
include Alchemy::Touching
include Alchemy::Hints
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Stores boolean values.
# Provides a checkbox in the editor views.
module Alchemy
class EssenceBoolean < ActiveRecord::Base
class EssenceBoolean < BaseRecord
acts_as_essence ingredient_column: 'value'
end
end
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#

module Alchemy
class EssenceDate < ActiveRecord::Base
class EssenceDate < BaseRecord
acts_as_essence ingredient_column: 'date'

# Returns self.date for the Element#preview_text method.
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

module Alchemy
class EssenceFile < ActiveRecord::Base
class EssenceFile < BaseRecord
belongs_to :attachment, required: false
acts_as_essence ingredient_column: 'attachment'

Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#

module Alchemy
class EssenceHtml < ActiveRecord::Base
class EssenceHtml < BaseRecord
acts_as_essence ingredient_column: 'source'

# Returns the first x (default = 30) (HTML escaped) characters from self.source for the Element#preview_text method.
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

module Alchemy
class EssenceLink < ActiveRecord::Base
class EssenceLink < BaseRecord
acts_as_essence ingredient_column: 'link'
end
end
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#

module Alchemy
class EssencePicture < ActiveRecord::Base
class EssencePicture < BaseRecord
acts_as_essence ingredient_column: 'picture'

belongs_to :picture, required: false
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_richtext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

module Alchemy
class EssenceRichtext < ActiveRecord::Base
class EssenceRichtext < BaseRecord
acts_as_essence preview_text_column: 'stripped_body'

before_save :strip_content
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Provides a select box that stores string values.
module Alchemy
class EssenceSelect < ActiveRecord::Base
class EssenceSelect < BaseRecord
acts_as_essence ingredient_column: 'value'
end
end
2 changes: 1 addition & 1 deletion app/models/alchemy/essence_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#

module Alchemy
class EssenceText < ActiveRecord::Base
class EssenceText < BaseRecord
acts_as_essence
end
end
2 changes: 1 addition & 1 deletion app/models/alchemy/folded_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#

module Alchemy
class FoldedPage < ActiveRecord::Base
class FoldedPage < BaseRecord
belongs_to :page, required: true
belongs_to :user, required: true,
class_name: Alchemy.user_class_name
Expand Down
22 changes: 19 additions & 3 deletions app/models/alchemy/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#

module Alchemy
class Language < ActiveRecord::Base
class Language < BaseRecord
belongs_to :site, required: true
has_many :pages

Expand All @@ -47,10 +47,10 @@ class Language < ActiveRecord::Base
if: -> { default_changed? && self != Language.default }

after_update :set_pages_language,
if: -> { language_code_changed? || country_code_changed? }
if: :should_set_pages_language?

after_update :unpublish_pages,
if: -> { changes[:public] == [true, false] }
if: :should_unpublish_pages?

before_destroy :check_for_default
after_destroy :delete_language_root_page
Expand Down Expand Up @@ -153,6 +153,14 @@ def remove_old_default
lang.save(validate: false)
end

def should_set_pages_language?
if active_record_5_1?
saved_change_to_language_code? || saved_change_to_country_code?
else
language_code_changed? || country_code_changed?
end
end

def set_pages_language
pages.update_all language_code: code
end
Expand All @@ -165,6 +173,14 @@ def delete_language_root_page
root_page.try(:destroy) && layout_root_page.try(:destroy)
end

def should_unpublish_pages?
if active_record_5_1?
saved_changes[:public] == [true, false]
else
changes[:public] == [true, false]
end
end

def unpublish_pages
pages.update_all(public_on: nil, public_until: nil)
end
Expand Down
19 changes: 16 additions & 3 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#

module Alchemy
class Page < ActiveRecord::Base
class Page < BaseRecord
include Alchemy::Hints
include Alchemy::Logger
include Alchemy::Touching
Expand Down Expand Up @@ -123,7 +123,7 @@ class Page < ActiveRecord::Base
unless: :systempage?

after_update :create_legacy_url,
if: :urlname_changed?,
if: :should_create_legacy_url?,
unless: :redirects_to_external?

# Concerns
Expand Down Expand Up @@ -483,9 +483,22 @@ def set_language_code
self.language_code = language.code
end

def should_create_legacy_url?
if active_record_5_1?
saved_change_to_urlname?
else
urlname_changed?
end
end

# Stores the old urlname in a LegacyPageUrl
def create_legacy_url
legacy_urls.find_or_create_by(urlname: urlname_was)
if active_record_5_1?
former_urlname = urlname_before_last_save
else
former_urlname = urlname_was
end
legacy_urls.find_or_create_by(urlname: former_urlname)
end

def set_published_at
Expand Down
16 changes: 14 additions & 2 deletions app/models/alchemy/page/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ module Page::PageElements
join_table: ElementToPage.table_name

after_create :autogenerate_elements, unless: -> { systempage? || do_not_autogenerate }
after_update :trash_not_allowed_elements!, if: :page_layout_changed?
after_update :autogenerate_elements, if: :page_layout_changed?

after_update :trash_not_allowed_elements!,
if: :has_page_layout_changed?

after_update :autogenerate_elements,
if: :has_page_layout_changed?
end

module ClassMethods
Expand Down Expand Up @@ -276,6 +280,14 @@ def trash_not_allowed_elements!
not_allowed_elements.to_a.map(&:trash!)
end

def has_page_layout_changed?
if active_record_5_1?
saved_change_to_page_layout?
else
page_layout_changed?
end
end

# Deletes unique and already present definitions from @_element_definitions.
#
def delete_unique_element_definitions!
Expand Down
21 changes: 17 additions & 4 deletions app/models/alchemy/page/page_naming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ module Page::PageNaming
validates :name,
presence: true
validates :urlname,
uniqueness: {scope: [:language_id, :layoutpage], if: 'urlname.present?'},
uniqueness: {scope: [:language_id, :layoutpage], if: -> { urlname.present? }},
exclusion: {in: RESERVED_URLNAMES},
length: {minimum: 3, if: 'urlname.present?'},
length: {minimum: 3, if: -> { urlname.present? }},
format: {with: /\A[:\.\w\-+_\/\?&%;=]*\z/, if: :redirects_to_external?}
validates :urlname,
on: :update,
presence: {if: :redirects_to_external?}

before_save :set_title, if: 'title.blank?', unless: proc { systempage? || redirects_to_external? }
before_save :set_title,
unless: -> { systempage? || redirects_to_external? },
if: -> { title.blank? }

after_update :update_descendants_urlnames,
if: -> { Config.get(:url_nesting) && (urlname_changed? || visible_changed?) }
if: :should_update_descendants_urlnames?

after_move :update_urlname!,
if: -> { Config.get(:url_nesting) },
unless: :redirects_to_external?
Expand Down Expand Up @@ -70,6 +74,15 @@ def visible_ancestors

private

def should_update_descendants_urlnames?
return false if !Config.get(:url_nesting)
if active_record_5_1?
saved_change_to_urlname? || saved_change_to_visible?
else
urlname_changed? || visible_changed?
end
end

def update_descendants_urlnames
reload
descendants.each do |descendant|
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#

module Alchemy
class Picture < ActiveRecord::Base
class Picture < BaseRecord
CONVERTIBLE_FILE_FORMATS = %w(gif jpg jpeg png).freeze

include Alchemy::NameConversions
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

module Alchemy
class Site < ActiveRecord::Base
class Site < BaseRecord
# validations
validates_presence_of :host
validates_uniqueness_of :host
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/essence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def acts_as_essence(options = {})
attr_writer :validation_errors
include Alchemy::Essence::InstanceMethods
stampable stamper_class_name: Alchemy.user_class_name
validate :validate_ingredient, :on => :update, :if => 'validations.any?'
validate :validate_ingredient, on: :update, if: -> { validations.any? }
has_one :content, :as => :essence, class_name: "Alchemy::Content"
has_one :element, :through => :content, class_name: "Alchemy::Element"
Expand Down
3 changes: 0 additions & 3 deletions spec/dummy/config/initializers/new_framework_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
# Require `belongs_to` associations by default. Previous versions had false.
Rails.application.config.active_record.belongs_to_required_by_default = true

# Do not halt callback chains when a callback returns false. Previous versions had true.
ActiveSupport.halt_callback_chains_on_return_false = false

# Configure SSL options to enable HSTS with subdomains. Previous versions had false.
Rails.application.config.ssl_options = { hsts: { subdomains: true } }

Expand Down

0 comments on commit 7c8a5b2

Please sign in to comment.