Skip to content

Commit

Permalink
Merge pull request #2409 from tvdeyen/element-definition-repository
Browse files Browse the repository at this point in the history
Add element definitions repository class option
  • Loading branch information
tvdeyen authored Jan 18, 2023
2 parents 1329cf3 + f3ff875 commit 13820ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/models/alchemy/element/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/models/alchemy/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 13820ae

Please sign in to comment.