From b9166964dab253d2574ddca11390187167334297 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Thu, 9 Nov 2023 09:08:06 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20Hyrax::GroupBeha?= =?UTF-8?q?vior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As of 2023-11-09, [Hyrax::Group in Hyku's main branch][1] inherits from ApplicationRecord. This means to share logic between the two requires that we extract a module (as we cannot change inheritance). This commit is that effort. [1]: https://github.com/samvera/hyku/blob/dd0da8061c2ee0233e4f32f8ea7748eda2792ba3/app/models/hyrax/group.rb#L6 --- app/models/hyrax/group.rb | 47 +++++------------------- app/models/hyrax/group_behavior.rb | 58 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 app/models/hyrax/group_behavior.rb diff --git a/app/models/hyrax/group.rb b/app/models/hyrax/group.rb index c8757133ed..7d174fcd96 100644 --- a/app/models/hyrax/group.rb +++ b/app/models/hyrax/group.rb @@ -1,49 +1,20 @@ # frozen_string_literal: true + module Hyrax + ## + # A Plain Old Ruby Object (PORO) representing a named group. + # + # In Hyku, we replace the PORO with an Application Record. But there is significant duplication + # of logic. + # + # @see Hyrax::GroupBehavior class Group - DEFAULT_NAME_PREFIX = 'group/' - - def self.name_prefix - DEFAULT_NAME_PREFIX - end - - ## - # @return [Hyrax::Group] - def self.from_agent_key(key) - new(key.delete_prefix(name_prefix)) - end + include Hyrax::GroupBehavior def initialize(name) @name = name end attr_reader :name - - ## - # @return [Boolean] - def ==(other) - other.class == self.class && other.name == name - end - - ## - # @return [String] a local identifier for this group; for use (e.g.) in ACL - # data - def agent_key - self.class.name_prefix + name - end - - def to_sipity_agent - sipity_agent || create_sipity_agent! - end - - private - - def sipity_agent - Sipity::Agent.find_by(proxy_for_id: name, proxy_for_type: self.class.name) - end - - def create_sipity_agent! - Sipity::Agent.create!(proxy_for_id: name, proxy_for_type: self.class.name) - end end end diff --git a/app/models/hyrax/group_behavior.rb b/app/models/hyrax/group_behavior.rb new file mode 100644 index 0000000000..c67fd9de73 --- /dev/null +++ b/app/models/hyrax/group_behavior.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module Hyrax + ## + # An {ActiveSupport::Concern} module that contains the {Hyrax::Group} logic. It is extracted to a + # behavior because downstream Hyku has a Hyrax::Group that inherits from ApplicationRecord. In + # other words it eschews the plain old Ruby object (PORO). However, both Hyku and Hyrax's + # {Hyrax::Group} both have notable amounts of duplicated logic. + # + # @see Hyrax::Group + module GroupBehavior + extend ActiveSupport::Concern + + DEFAULT_NAME_PREFIX = 'group/' + + class_methods do + ## + # @return [String] + # @see DEFAULT_NAME_PREFIX + def name_prefix + DEFAULT_NAME_PREFIX + end + + ## + # @return [Hyrax::Group] + def from_agent_key(key) + new(key.delete_prefix(name_prefix)) + end + end + + ## + # @return [Boolean] + def ==(other) + other.class == self.class && other.name == name + end + + ## + # @return [String] a local identifier for this group; for use (e.g.) in ACL + # data + def agent_key + self.class.name_prefix + name + end + + def to_sipity_agent + sipity_agent || create_sipity_agent! + end + + private + + def sipity_agent + Sipity::Agent.find_by(proxy_for_id: name, proxy_for_type: self.class.name) + end + + def create_sipity_agent! + Sipity::Agent.create!(proxy_for_id: name, proxy_for_type: self.class.name) + end + end +end