diff --git a/app/models/alchemy/element/definitions.rb b/app/models/alchemy/element/definitions.rb index 3455b8d599..df57d23e33 100644 --- a/app/models/alchemy/element/definitions.rb +++ b/app/models/alchemy/element/definitions.rb @@ -8,19 +8,33 @@ module Definitions extend ActiveSupport::Concern module ClassMethods + # Register a custom element definitions repository + # + # The default repository is Alchemy::ElementDefinition + # + def definitions_repository=(klass) + @_definitions_repository = klass + end + # Returns the definitions from elements.yml file. # # Place a +elements.yml+ file inside your apps +config/alchemy+ folder to define # your own set of elements # def definitions - ElementDefinition.all + definitions_repository.all end # Returns one element definition by given name. # def definition_by_name(name) - ElementDefinition.get(name) + definitions_repository.get(name) + end + + private + + def definitions_repository + @_definitions_repository ||= ElementDefinition end end diff --git a/spec/models/alchemy/element_spec.rb b/spec/models/alchemy/element_spec.rb index 037da37cbe..c0c690177d 100644 --- a/spec/models/alchemy/element_spec.rb +++ b/spec/models/alchemy/element_spec.rb @@ -310,6 +310,16 @@ module Alchemy end end + describe ".definitions_repository=" do + let(:dummy_repo) { Class.new } + + it "should be able to set another repository class" do + expect(Element.definitions_repository = dummy_repo).to eq(dummy_repo) + end + + after { Element.instance_variable_set(:@_definitions_repository, nil) } + end + describe ".display_name_for" do it "should return the translation for the given name" do expect(Alchemy).to receive(:t).with("subheadline", scope: "element_names", default: "Subheadline").and_return("Überschrift")