Sha256: 1f55a39a9786d575e4c3e64b2ec3efd3882b603b3eb282e634a3c64dceb5c1db

Contents?: true

Size: 1.34 KB

Versions: 61

Compression:

Stored size: 1.34 KB

Contents

require 'active_attr'
module CurationConcerns
  class ClassifyConcern
    include ActiveAttr::Model
    attribute :curation_concern_type

    validates(
      :curation_concern_type,
      presence: true,
      inclusion: { in: ->(record) { record.registered_curation_concern_types } }
    )

    def all_curation_concern_classes
      registered_curation_concern_types.sort.map { |c| self.class.to_class(c) }
    end

    def registered_curation_concern_types
      CurationConcerns.config.registered_curation_concern_types
    end

    def possible_curation_concern_types
      registered_curation_concern_types.collect do |concern|
        [self.class.to_class(concern).human_readable_type, concern]
      end
    end

    def curation_concern_class
      if possible_curation_concern_types.detect do |_name, class_name|
        class_name == curation_concern_type
      end
        self.class.to_class(curation_concern_type)
      else
        fail 'Invalid :curation_concern_type'
      end
    end

    # @option [String] type name of the model
    # @return [Class] the model class
    def self.to_class(type)
      # TODO: we may want to allow a different (or nil) namespace
      type.camelize.constantize
      # begin
      #   "::#{type.camelize}".constantize
      # rescue NameError
      #   "CurationConcerns::#{type}".constantize
      # end
    end
  end
end

Version data entries

61 entries across 61 versions & 2 rubygems

Version Path
curation_concerns-1.7.8 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.7 app/models/curation_concerns/classify_concern.rb
curation_concerns-2.0.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-2.0.0.rc2 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.6 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.5 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.4 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.3 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.2 app/models/curation_concerns/classify_concern.rb
curation_concerns-2.0.0.rc1 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.1 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.7.0.beta1 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.6.3 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.6.2 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.6.1 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.6.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.5.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.4.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-1.3.3 app/models/curation_concerns/classify_concern.rb