Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add element definitions repository class option #2409

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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