From c4a04586c4884ecc3a2ac21ed2a93487a9c13b77 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 16 Jan 2021 16:20:27 +0100 Subject: [PATCH] Add element definitions repository class option This allows to configure a custom element definitions repository class. --- app/models/alchemy/element/definitions.rb | 18 ++++++++++++++++-- spec/models/alchemy/element_spec.rb | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/models/alchemy/element/definitions.rb b/app/models/alchemy/element/definitions.rb index 3455b8d599..ff4286baba 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 + @_repository ||= ElementDefinition end end diff --git a/spec/models/alchemy/element_spec.rb b/spec/models/alchemy/element_spec.rb index 037da37cbe..e04e3c3bd5 100644 --- a/spec/models/alchemy/element_spec.rb +++ b/spec/models/alchemy/element_spec.rb @@ -310,6 +310,14 @@ 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 + 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")