Skip to content

Commit

Permalink
Content node form builder improved
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Jan 21, 2015
1 parent 1643c54 commit 588be2b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,15 @@
.controller-releaf-content-nodes.view-edit .node-fields .field[data-name="active"]
{
min-height: initial;
clear: both;
}
.controller-releaf-content-nodes.view-edit .node-fields .field[data-name="active"] .value
{
padding-top: initial;
}
.controller-releaf-content-nodes.view-edit .node-fields hr
{
clear: both;
border: 1px solid #d4d4d4;
margin: 7px 34px 7px 0px;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def prepare_node
@resource.item_position ||= resource_class.children_max_item_position(@resource.parent) + 1

if node_content_class < ActiveRecord::Base
@resource.build_content({})
@resource.build_content
@resource.content_id_will_change!
end
end
Expand Down
13 changes: 0 additions & 13 deletions releaf-content/app/helpers/releaf/content/nodes/edit_builder.rb

This file was deleted.

38 changes: 36 additions & 2 deletions releaf-content/app/helpers/releaf/content/nodes/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
module Releaf::Content::Nodes
class FormBuilder < Releaf::Builders::FormBuilder
def field_names
%w(node_fields_block content_fields_block)
end

def node_fields
[:parent_id, :name, :content_type, :slug, :item_position, :active, :locale]
end

def render_node_fields_block
tag(:div, class: ["section", "node-fields", "clear-inside"]) do
releaf_fields(node_fields)
end
end

def render_parent_id
hidden_field(:parent_id) if object.new_record?
end

def content_fields
#binding.pry
object.content_class.releaf_fields_to_display(nil) if object.content_class.respond_to?(:releaf_fields_to_display)
end

def render_content_fields_block
return unless content_fields.present?
tag(:div, class: ["section", "content-fields"]) do
fields_for(:content, object.content) do |form|
form.releaf_fields(content_fields)
end
end
end

def render_locale
releaf_item_field(:locale, options: render_locale_options)
releaf_item_field(:locale, options: render_locale_options) if object.locale_selection_enabled?
end

def render_locale_options
Expand All @@ -13,7 +45,9 @@ def render_locale_options

def render_content_type
input = {disabled: true, value: t(object.content_type.underscore, scope: 'admin.content_types')}
releaf_text_field(:content_type, input: input)
releaf_text_field(:content_type, input: input) do
hidden_field_tag(:content_type, params[:content_type]) if object.new_record?
end
end

def render_slug
Expand Down
24 changes: 0 additions & 24 deletions releaf-content/app/views/releaf/content/nodes/_form_fields.haml

This file was deleted.

18 changes: 2 additions & 16 deletions releaf-content/lib/releaf/content/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,12 @@ def locale_selection_enabled?
false
end

def build_content(params, assignment_options=nil)
def build_content(params = {}, assignment_options = nil)
self.content = content_class.new(params)
end

def own_fields_to_display
[]
end

def content_fields_to_display action
if content_class.respond_to? :releaf_fields_to_display
content_class.releaf_fields_to_display(action)
else
nil
end
end

def content_class
return nil if content_type.blank?
content_type.constantize
content_type.constantize unless content_type.blank?
end

##
Expand Down Expand Up @@ -66,7 +53,6 @@ def attributes_to_copy
self.class.column_names - attributes_to_not_copy
end


def copy parent_id
prevent_infinite_copy_loop(parent_id)
begin
Expand Down
98 changes: 94 additions & 4 deletions releaf-content/spec/helpers/content/nodes/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,101 @@ def generate_url_releaf_content_nodes_path(args); end
parent: Node.new(content_type: "Text", slug: "a", id: 1)) }
let(:subject){ described_class.new(:resource, object, template, {}) }

describe "#field_names" do
it "returns hidden, node and content object fields" do
expect(subject.field_names).to eq(["node_fields_block", "content_fields_block"])
end
end

describe "#node_fields" do
it "returns array with renderable node fields" do
list = [:parent_id, :name, :content_type, :slug, :item_position, :active, :locale]
expect(subject.node_fields).to eq(list)
end
end

describe "#render_node_fields_block" do
it "renders node fields" do
allow(subject).to receive(:node_fields).and_return([1, 2])
allow(subject).to receive(:releaf_fields).with([1, 2]).and_return("x")
content = '<div class="section node-fields clear-inside">x</div>'
expect(subject.render_node_fields_block).to eq(content)
end
end

describe "#render_parent_id" do
it "renders hidden parent if field for new object" do
allow(subject.object).to receive(:new_record?).and_return(true)
allow(subject).to receive(:hidden_field).with(:parent_id).and_return("x")
expect(subject.render_parent_id).to eq("x")
end

it "renders nothing for existing object" do
allow(subject.object).to receive(:new_record?).and_return(false)
expect(subject.render_parent_id).to eq(nil)
end
end

describe "#content_fields" do
before do
subject.object.build_content
end

it "returns array of node content object fields" do
allow(object.content_class).to receive(:respond_to?).with(:releaf_fields_to_display).and_return(true)
allow(object.content_class).to receive(:respond_to?).with(:releaf_fields_to_display, true).and_return(true)
allow(object.content_class).to receive(:releaf_fields_to_display).and_return(["a", "b"])
expect(subject.content_fields).to eq(["a", "b"])
end

context "when object content class do not respond to releaf_fields_to_display" do
it "returns nil" do
allow(object.content_class).to receive(:respond_to?).with(:releaf_fields_to_display).and_return(false)
expect(subject.content_fields).to be nil
end
end
end

describe "#render_content_fields_block" do
before do
subject.object.build_content
end

it "renders content fields block" do
allow(subject).to receive(:content_fields).and_return([1, 2])
subform = described_class.new(:resource, object, template, {})
allow(subject).to receive(:fields_for).with(:content, subject.object.content).and_yield(subform)
allow(subform).to receive(:releaf_fields).with([1, 2]).and_return("yy")
content = '<div class="section content-fields">yy</div>'
expect(subject.render_content_fields_block).to eq(content)
end

context "when content have no fields" do
it "returns nil" do
allow(subject).to receive(:content_fields).and_return([])
expect(subject.render_content_fields_block).to be nil

allow(subject).to receive(:content_fields).and_return(nil)
expect(subject.render_content_fields_block).to be nil
end
end
end

describe "#render_locale" do
it "renders locale with #render_locale_options" do
allow(subject).to receive(:render_locale_options).and_return(a: "b")
allow(subject).to receive(:releaf_item_field).with(:locale, options: {a: "b"}).and_return("x")
expect(subject.render_locale).to eq("x")
context "when node node has locale select enabled" do
it "renders locale with #render_locale_options" do
allow(subject.object).to receive(:locale_selection_enabled?).and_return(true)
allow(subject).to receive(:render_locale_options).and_return(a: "b")
allow(subject).to receive(:releaf_item_field).with(:locale, options: {a: "b"}).and_return("x")
expect(subject.render_locale).to eq("x")
end
end

context "when node node does not have locale select enabled" do
it "renders locale with #render_locale_options" do
allow(subject.object).to receive(:locale_selection_enabled?).and_return(false)
expect(subject.render_locale).to be nil
end
end
end

Expand Down
30 changes: 0 additions & 30 deletions releaf-content/spec/models/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,6 @@
end
end

describe "#own_fields_to_display" do
it "returns blank array" do
expect( node.own_fields_to_display ).to eq []
end
end

describe "#content_fields_to_display" do
context "when #content_type is name of non ActiveRecord class" do
it "returns nil" do
node.content_type = 'TrueClass'
expect( node.content_fields_to_display('edit') ).to be_nil
end
end

context "when #content_type is name of ActionController class" do
it "returns nil" do
node.content_type = 'TextsController'
expect( node.content_fields_to_display('edit') ).to be_nil
end
end

context "when #content_type is name of ActiveRecord model that acts as node" do
it "display all but #id, #updated_at and #created_at fields" do
node.content_type = 'Book'
expect( node.content_fields_to_display('edit') ).to match_array(Book.column_names - %w[id created_at updated_at])
end
end

end

describe "#content_class" do
context 'when #content_type is nil' do
it 'returns nil' do
Expand Down

0 comments on commit 588be2b

Please sign in to comment.