From 2ab49a18326c1e6a0235be48fa9fac20e852964d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 11 Oct 2018 02:29:08 +0200 Subject: [PATCH] Refactor globalize models code using a concern I've chosen the name "Globalizable" because "Translatable" already existed. --- app/models/admin_notification.rb | 3 +-- app/models/banner.rb | 3 +-- app/models/budget/investment/milestone.rb | 3 +-- app/models/concerns/globalizable.rb | 8 ++++++++ app/models/legislation/draft_version.rb | 3 +-- app/models/legislation/process.rb | 3 +-- app/models/legislation/question.rb | 3 +-- app/models/legislation/question_option.rb | 3 +-- app/models/poll.rb | 3 +-- app/models/poll/question.rb | 3 +-- app/models/poll/question/answer.rb | 3 +-- app/models/site_customization/page.rb | 3 +-- app/models/widget/card.rb | 3 +-- 13 files changed, 20 insertions(+), 24 deletions(-) create mode 100644 app/models/concerns/globalizable.rb diff --git a/app/models/admin_notification.rb b/app/models/admin_notification.rb index e53150ace..69c195c17 100644 --- a/app/models/admin_notification.rb +++ b/app/models/admin_notification.rb @@ -3,8 +3,7 @@ class AdminNotification < ActiveRecord::Base translates :title, touch: true translates :body, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable translation_class.instance_eval do validates :title, presence: true diff --git a/app/models/banner.rb b/app/models/banner.rb index 10399211b..1f3757644 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -5,8 +5,7 @@ class Banner < ActiveRecord::Base translates :title, touch: true translates :description, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable translation_class.instance_eval do validates :title, presence: true, length: { minimum: 2 } diff --git a/app/models/budget/investment/milestone.rb b/app/models/budget/investment/milestone.rb index 84b312867..c71516446 100644 --- a/app/models/budget/investment/milestone.rb +++ b/app/models/budget/investment/milestone.rb @@ -8,8 +8,7 @@ class Budget accepted_content_types: [ "application/pdf" ] translates :title, :description, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable belongs_to :investment belongs_to :status, class_name: 'Budget::Investment::Status' diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb new file mode 100644 index 000000000..39b50f81f --- /dev/null +++ b/app/models/concerns/globalizable.rb @@ -0,0 +1,8 @@ +module Globalizable + extend ActiveSupport::Concern + + included do + globalize_accessors + accepts_nested_attributes_for :translations, allow_destroy: true + end +end diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb index 9e8ded1eb..5b594dd7b 100644 --- a/app/models/legislation/draft_version.rb +++ b/app/models/legislation/draft_version.rb @@ -9,8 +9,7 @@ class Legislation::DraftVersion < ActiveRecord::Base translates :body, touch: true translates :body_html, touch: true translates :toc_html, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id' has_many :annotations, class_name: 'Legislation::Annotation', foreign_key: 'legislation_draft_version_id', dependent: :destroy diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 9da62403d..51302fbb1 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -13,8 +13,7 @@ class Legislation::Process < ActiveRecord::Base translates :summary, touch: true translates :description, touch: true translates :additional_info, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable PHASES_AND_PUBLICATIONS = %i(debate_phase allegations_phase proposals_phase draft_publication result_publication).freeze diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index c98c1e3bd..8d01e4a4f 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -4,8 +4,7 @@ class Legislation::Question < ActiveRecord::Base include Notifiable translates :title, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id' diff --git a/app/models/legislation/question_option.rb b/app/models/legislation/question_option.rb index a839d400f..0ca583478 100644 --- a/app/models/legislation/question_option.rb +++ b/app/models/legislation/question_option.rb @@ -3,8 +3,7 @@ class Legislation::QuestionOption < ActiveRecord::Base include ActsAsParanoidAliases translates :value, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :question_options has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question diff --git a/app/models/poll.rb b/app/models/poll.rb index 8b5dad7dd..6d062a86b 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -7,8 +7,7 @@ class Poll < ActiveRecord::Base translates :name, touch: true translates :summary, touch: true translates :description, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable RECOUNT_DURATION = 1.week diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index e8a3e0dd7..e253aab36 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -6,8 +6,7 @@ class Poll::Question < ActiveRecord::Base include ActsAsParanoidAliases translates :title, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable belongs_to :poll belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 5fd0e19c7..8faf9a0c5 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -4,8 +4,7 @@ class Poll::Question::Answer < ActiveRecord::Base translates :title, touch: true translates :description, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable documentable max_documents_allowed: 3, max_file_size: 3.megabytes, diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index a17345ba0..b0c18c96a 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -4,8 +4,7 @@ class SiteCustomization::Page < ActiveRecord::Base translates :title, touch: true translates :subtitle, touch: true translates :content, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable translation_class.instance_eval do validates :title, presence: true diff --git a/app/models/widget/card.rb b/app/models/widget/card.rb index 0f8e176a4..4a7733e24 100644 --- a/app/models/widget/card.rb +++ b/app/models/widget/card.rb @@ -8,8 +8,7 @@ class Widget::Card < ActiveRecord::Base translates :title, touch: true translates :description, touch: true translates :link_text, touch: true - globalize_accessors - accepts_nested_attributes_for :translations, allow_destroy: true + include Globalizable def self.header where(header: true)