Skip to content

Commit

Permalink
Merge pull request #6427 from samvera/extract-hyrax-group-behavior
Browse files Browse the repository at this point in the history
♻️ Extract Hyrax::GroupBehavior
  • Loading branch information
dlpierce committed Nov 10, 2023
2 parents 8d7b0ff + b916696 commit 74f1e40
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
47 changes: 9 additions & 38 deletions app/models/hyrax/group.rb
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions app/models/hyrax/group_behavior.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 74f1e40

Please sign in to comment.