From f3ff8758a3c64b2313d0d88805df6486ee4885e9 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 | 10 ++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) 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")