Sha256: e3971426ac84584b0154f82fa77c28f238cc6ae05e3711d8af904b170a14dd07

Contents?: true

Size: 1.34 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
curation_concerns-models-0.6.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-models-0.5.0 app/models/curation_concerns/classify_concern.rb
curation_concerns-models-0.4.0 app/models/curation_concerns/classify_concern.rb