Skip to content

Commit

Permalink
Merge pull request #1076 from texpert/fix-active-record-deprecations
Browse files Browse the repository at this point in the history
Fix ActiveRecord deprecations from Rails 6.1
  • Loading branch information
texpert authored Jul 26, 2024
2 parents 4267977 + 3aed50b commit 53cdef5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

## Unreleased
- Use jQuery 2.x - 2.2.4
- If there are `//= require jquery` clauses in the main application, replace them with `//= require jquery2`
- **If there are `//= require jquery` clauses in the main application, replace them with `//= require jquery2`**
- Add Ruby 3.3 and Rails 7.2 to CI
- Replace Tuzitio links with `camaleon.website` and http with https
- Replace Tuzitio links with `camaleon.website` and `http` with `https`
- On cama_site_check_existence, if site is unknown, use `allow_other_host: true` for redirection to main site
- Starting from Rails 7.0 a redirection to other host will raise an exception unless the `redirect_to` method is
called with the `allow_other_host: true` option
- Set sprocket-rails version to be at least 3.5.1
- Use MiniMime for mime types, because the MiniMagick 5.0 has no Image#mime_type
- Reimplement the temporary uploaded file removing, wrapping it in a bl…ock to make possible overriding the block in the app initializer to use an async job
- Sanitize name and description attrs of TermTaxonomy classes to prevent XSS attacks
- **Potentially breaking change:** Fix ActiveRecord deprecations from Rails 6.1
- `fields`, `field_values`, and `field_groups` associations have been removed from the CustomFieldsRead mixin module
- `custom_fields`, `custom_field_values`, and `custom_field_groups` associations should be used instead
- Beware that the CustomFieldsRead mixin is included into the TermTaxonomy base model, PostDefault model, and UserMethods mixin

## [2.7.5](https://github.com/owen2345/camaleon-cms/tree/2.7.5) (2023-11-22)
- Fix the test email for non-main sites by [brian-kephart](https://github.com/brian-kephart) in [\#1050](https://github.com/owen2345/camaleon-cms/pull/1050)
Expand Down
4 changes: 2 additions & 2 deletions app/models/camaleon_cms/custom_field_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class CustomFieldGroup < CamaleonCms::CustomField
alias_attribute :site_id, :parent_id

default_scope do
where.not(object_class: '_fields')
.reorder("#{CamaleonCms::CustomField.table_name}.field_order ASC")
where("object_class != '_fields'")
.reorder("#{CamaleonCms::CustomField.table_name}.field_order ASC")
end

has_many :metas, -> { where(object_class: 'CustomFieldGroup') }, foreign_key: :objectid, dependent: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/camaleon_cms/post_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def self.find_by_slug(slug)
res = where("#{CamaleonCms::Post.table_name}.slug = ? OR #{CamaleonCms::Post.table_name}.slug LIKE ? ", slug,
"%-->#{slug}<!--%")
# end
res.reorder('').first
res.take
end

# return the parent of a post (support for sub contents or tree of posts)
Expand Down
29 changes: 9 additions & 20 deletions app/models/concerns/camaleon_cms/custom_fields_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ module CustomFieldsRead
extend ActiveSupport::Concern
included do
before_destroy :_destroy_custom_field_groups
# DEPRECATED, INSTEAD USE: custom_fields
has_many :fields, lambda { |object|
where(object_class: object.class.to_s.gsub('Decorator', '').gsub('CamaleonCms::', ''))
}, class_name: 'CamaleonCms::CustomField', foreign_key: :objectid
# DEPRECATED, INSTEAD USE: custom_field_values
has_many :field_values, lambda { |object|
where(object_class: object.class.to_s.gsub('Decorator', '').gsub('CamaleonCms::', ''))
}, class_name: 'CamaleonCms::CustomFieldsRelationship', foreign_key: :objectid, dependent: :delete_all
# DEPRECATED, INSTEAD USE: custom_field_groups
has_many :field_groups, lambda { |object|
where(object_class: object.class.to_s.parseCamaClass)
}, class_name: 'CamaleonCms::CustomFieldGroup', foreign_key: :objectid
end

# get custom field groups for current object
Expand All @@ -38,7 +26,7 @@ def get_field_groups(args = {})
{ kind: args,
include_parent: false }
else
{ kind: 'Post', include_parent: false }.merge(args)
{ kind: 'Post', include_parent: false }.merge!(args)
end
class_name = self.class.to_s.parseCamaClass
case class_name
Expand Down Expand Up @@ -73,7 +61,7 @@ def get_field_groups(args = {})
CamaleonCms::CustomFieldGroup.where(object_class: "PostType_#{args[:kind]}", objectid: id)
end
else # 'Plugin' or other classes
field_groups
custom_field_groups
end
end

Expand Down Expand Up @@ -196,8 +184,8 @@ def get_fields_object(_f = true)
# kind: argument only for PostType model: (Post | Category | PostTag), default => Post. If kind = "" this will add group for all post_types
def add_custom_field_group(values, kind = 'Post')
values = values.with_indifferent_access
group = get_field_groups(kind).where(slug: values[:slug]).first
unless group.present?
group = get_field_groups(kind).find_by(slug: values[:slug])
unless group
site = _cama_get_field_site
values[:parent_id] = site.id if site.present?
group = if is_a?(CamaleonCms::Post) # harcoded for post to support custom field groups
Expand All @@ -206,6 +194,7 @@ def add_custom_field_group(values, kind = 'Post')
get_field_groups(kind).create!(values)
end
end

group
end
alias add_field_group add_custom_field_group
Expand All @@ -215,8 +204,8 @@ def add_custom_field_group(values, kind = 'Post')
# more details in add_manual_field(item, options) from custom field groups
# kind: argument only for PostType model: (Post | Category | PostTag), default => Post
def add_custom_field_to_default_group(item, options, kind = 'Post')
g = get_field_groups(kind).where(slug: '_default').first
g = add_custom_field_group({ name: 'Default Field Group', slug: '_default' }, kind) unless g.present?
g = get_field_groups(kind).find_by(slug: '_default')
g ||= add_custom_field_group({ name: 'Default Field Group', slug: '_default' }, kind)
g.add_manual_field(item, options)
end
alias add_field add_custom_field_to_default_group
Expand Down Expand Up @@ -260,7 +249,7 @@ def set_field_values(datas = {})
# update new value for field with slug _key
# Sample: my_posy.update_field_value('sub_title', 'Test Sub Title')
def update_field_value(_key, value = nil, group_number = 0)
custom_field_values.where(custom_field_slug: _key, group_number: group_number).first.update_column('value', value)
custom_field_values.find_by(custom_field_slug: _key, group_number: group_number)&.update_column('value', value)
rescue StandardError
nil
end
Expand All @@ -287,7 +276,7 @@ def save_field_value(key, value, order = 0, clear = true)
# sample: my_post.set_field_value('subtitle', 'Sub Title', {group_number: 1})
# sample: my_post.set_field_value('subtitle', 'Sub Title', {group_number: 1, group_number: 1}) # add field values for fields in group 1
def set_field_value(key, value, args = {})
args = { order: 0, group_number: 0, field_id: nil, clear: true }.merge(args)
args = { order: 0, group_number: 0, field_id: nil, clear: true }.merge!(args)
unless args[:field_id].present?
args[:field_id] = begin
get_field_object(key).id
Expand Down

0 comments on commit 53cdef5

Please sign in to comment.