Skip to content

Commit 1f0d155

Browse files
committed
Replace class constants with class attributes for overridable class values
1 parent aea049b commit 1f0d155

File tree

6 files changed

+31
-35
lines changed

6 files changed

+31
-35
lines changed

app/helpers/better_together/navigation_items_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def render_platform_header_nav_items
109109

110110
def route_names_for_select(nav_item = nil)
111111
options_for_select(
112-
BetterTogether::NavigationItem::ROUTE_NAMES.map { |name, route| [I18n.t("route_names.#{name}"), route] },
112+
BetterTogether::NavigationItem.route_names.map { |name, route| [I18n.t("route_names.#{name}"), route] },
113113
(nav_item ? nav_item.route_name : nil)
114114
)
115115
end

app/models/better_together/content/block.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def to_partial_path
2828
def self.inherited(subclass)
2929
super
3030
# Your custom logic here, which will be available to all subclasses
31-
subclass.instance_eval do
32-
include ::BetterTogether::Content::BlockAttributes
33-
end
31+
subclass.include ::BetterTogether::Content::BlockAttributes
3432
end
3533

3634
def self.block_name

app/models/better_together/content/template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module BetterTogether
44
module Content
55
# Renders a template from a file
66
class Template < Block
7-
AVAILABLE_TEMPLATES = %w[
7+
class_attribute :available_templates, default: %w[
88
better_together/content/blocks/template/default
99
better_together/content/blocks/template/host_community_contact_details
10-
].freeze
10+
]
1111

1212
has_many :page_blocks, foreign_key: :block_id, dependent: :destroy
1313
has_many :pages, through: :page_blocks
@@ -16,7 +16,7 @@ class Template < Block
1616
template_path String
1717
end
1818

19-
validates :template_path, presence: true, inclusion: { in: ->(template) { template.class::AVAILABLE_TEMPLATES } }
19+
validates :template_path, presence: true, inclusion: { in: ->(template) { template.class.available_templates } }
2020
end
2121
end
2222
end

app/models/better_together/navigation_item.rb

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ class NavigationItem < ApplicationRecord # rubocop:todo Metrics/ClassLength
77
include Positioned
88
include Protected
99

10+
class_attribute :route_names, default: {
11+
content_blocks: 'content_blocks_path',
12+
communities: 'communities_path',
13+
geography_continents: 'geography_continents_path',
14+
geography_countries: 'geography_countries_path',
15+
geography_states: 'geography_states_path',
16+
geography_regions: 'geography_regions_path',
17+
geography_settlements: 'geography_settlements_path',
18+
host_dashboard: 'host_dashboard_path',
19+
metrics_reports: 'metrics_reports_path',
20+
navigation_areas: 'navigation_areas_path',
21+
pages: 'pages_path',
22+
people: 'people_path',
23+
platforms: 'platforms_path',
24+
resource_permissions: 'resource_permissions_path',
25+
roles: 'roles_path',
26+
users: 'users_path'
27+
}
28+
1029
belongs_to :navigation_area, touch: true
1130
belongs_to :linkable, polymorphic: true, optional: true, autosave: true
1231

@@ -31,27 +50,8 @@ class NavigationItem < ApplicationRecord # rubocop:todo Metrics/ClassLength
3150
'BetterTogether::Page'
3251
].freeze
3352

34-
ROUTE_NAMES = {
35-
content_blocks: 'content_blocks_path',
36-
communities: 'communities_path',
37-
geography_continents: 'geography_continents_path',
38-
geography_countries: 'geography_countries_path',
39-
geography_states: 'geography_states_path',
40-
geography_regions: 'geography_regions_path',
41-
geography_settlements: 'geography_settlements_path',
42-
host_dashboard: 'host_dashboard_path',
43-
metrics_reports: 'metrics_reports_path',
44-
navigation_areas: 'navigation_areas_path',
45-
pages: 'pages_path',
46-
people: 'people_path',
47-
platforms: 'platforms_path',
48-
resource_permissions: 'resource_permissions_path',
49-
roles: 'roles_path',
50-
users: 'users_path'
51-
}.freeze
52-
5353
def self.route_name_paths
54-
ROUTE_NAMES.values.map(&:to_s)
54+
route_names.values.map(&:to_s)
5555
end
5656

5757
translates :title, type: :string
@@ -97,8 +97,7 @@ def self.route_name_paths
9797
combined_conditions = visible_flag.and(not_page.or(published_page).or(linkable_is_nil))
9898

9999
# Apply the join and where conditions
100-
joins(join)
101-
.where(combined_conditions)
100+
joins(join).where(combined_conditions)
102101
}
103102

104103
def build_children(pages, navigation_area) # rubocop:todo Metrics/MethodLength

app/models/concerns/better_together/removeable_attachment.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
module BetterTogether
44
module RemoveableAttachment # rubocop:todo Style/Documentation
55
extend ::ActiveSupport::Concern
6+
67
included do
7-
# rubocop:todo Lint/ConstantDefinitionInBlock
8-
ATTACHMENT_ATTRIBUTES = [] # rubocop:todo Style/MutableConstant, Lint/ConstantDefinitionInBlock
9-
# rubocop:enable Lint/ConstantDefinitionInBlock
8+
class_attribute :attachment_attributes, default: []
109

1110
# define accessors, before_save callback, and purge method for all declared has_one attachments
1211
reflect_on_all_attachments
@@ -17,7 +16,7 @@ module RemoveableAttachment # rubocop:todo Style/Documentation
1716
remove_attachment_attr = :"remove_#{attachment}"
1817
attr_accessor :"remove_#{attachment}"
1918

20-
ATTACHMENT_ATTRIBUTES.push(attachment, remove_attachment_attr)
19+
attachment_attributes.push(attachment, remove_attachment_attr)
2120

2221
# Callbacks to remove images if necessary
2322
before_save :"purge_#{attachment}", if: -> { public_send(remove_attachment_attr) == '1' }
@@ -30,7 +29,7 @@ module RemoveableAttachment # rubocop:todo Style/Documentation
3029

3130
class_methods do
3231
def extra_permitted_attributes
33-
super + ATTACHMENT_ATTRIBUTES.flatten.compact
32+
super + attachment_attributes.flatten.compact
3433
end
3534
end
3635
end

app/views/better_together/content/blocks/fields/_template.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
<%- scope = local_assigns[:scope] ? local_assigns[:scope] : BetterTogether::Content::Block.block_name %>
33
<div class="template-fields">
44
<%= required_label block, :template_path %>
5-
<%= select_tag "#{scope}[template_path]", options_for_select(block.class::AVAILABLE_TEMPLATES.map { |path| [ I18n.t(path.tr('/', '.')), path] }, block.template_path), class: 'form-select' %>
5+
<%= select_tag "#{scope}[template_path]", options_for_select(block.class::available_templates.map { |path| [ I18n.t(path.tr('/', '.')), path] }, block.template_path), class: 'form-select' %>
66
</div>

0 commit comments

Comments
 (0)