-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This Essence can be used to add a menu and its children to an Element.
- Loading branch information
Showing
8 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class EssenceNode < BaseRecord | ||
NODE_ID = /\A\d+\z/ | ||
|
||
acts_as_essence( | ||
ingredient_column: :node, | ||
preview_text_column: :node_name, | ||
belongs_to: { | ||
class_name: "Alchemy::Node", | ||
foreign_key: :node_id, | ||
inverse_of: :essence_nodes, | ||
optional: true | ||
} | ||
) | ||
|
||
delegate :name, to: :node, prefix: true | ||
|
||
def ingredient=(node) | ||
case node | ||
when NODE_ID | ||
self.node = Alchemy::Page.new(id: node) | ||
when Alchemy::Node | ||
self.node = node | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<%# | ||
Available locals: | ||
* content (The object the essence is linked to the element) | ||
* html_options | ||
Please consult Alchemy::Content.rb docs for further methods on the content object | ||
%> | ||
<div class="content_editor essence_node" id="<%= content.dom_id %>" data-content-id="<%= content.id %>"> | ||
<%= content_label(content) %> | ||
<%= select_tag( | ||
content.form_field_name, | ||
options_from_collection_for_select( | ||
local_assigns.fetch(:options, {})[:nodes] || Alchemy::Node.where(site: Alchemy::Site.current), | ||
:id, | ||
:name, | ||
content.essence.node_id | ||
), | ||
include_blank: t(".none"), | ||
class: "alchemy_selectbox essence_editor_select full_width" | ||
) %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<%# The content object holds the essence %> | ||
<%= content.ingredient %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateAlchemyEssenceNodes < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :alchemy_essence_nodes do |t| | ||
t.integer "node_id" | ||
t.index ["node_id"], name: "index_alchemy_essence_nodes_on_node_id" | ||
t.timestamps | ||
end | ||
add_foreign_key "alchemy_essence_nodes", "alchemy_nodes", column: "node_id" | ||
end | ||
end |
1 change: 1 addition & 0 deletions
1
spec/dummy/db/migrate/20200423073425_create_alchemy_essence_nodes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../db/migrate/20200423073425_create_alchemy_essence_nodes.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Alchemy::EssenceNode, type: :model do | ||
it { is_expected.to belong_to(:ingredient_association).optional.class_name('Alchemy::Node') } | ||
|
||
describe '#ingredient' do | ||
let(:node) { build(:alchemy_node) } | ||
|
||
subject { described_class.new(node: node).ingredient } | ||
|
||
it { is_expected.to eq(node) } | ||
end | ||
|
||
describe '#preview_text' do | ||
let(:node) { build(:alchemy_node) } | ||
|
||
subject { described_class.new(node: node).preview_text } | ||
|
||
it { is_expected.to eq(node.name) } | ||
end | ||
end |