Skip to content

Commit

Permalink
Make it possible to add custom validators to node via acts_as_node
Browse files Browse the repository at this point in the history
  • Loading branch information
graudeejs committed Apr 9, 2014
1 parent 0759c3d commit 296e37d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
20 changes: 18 additions & 2 deletions app/models/releaf/node.rb
Expand Up @@ -15,15 +15,21 @@ class Node < ActiveRecord::Base

alias_attribute :to_text, :name

belongs_to :content, polymorphic: true, dependent: :destroy, class_name: Proc.new{|r| r.content_type.constantize}
belongs_to :content, polymorphic: true, dependent: :destroy, class_name: Proc.new{ |r| r.content_class }
accepts_nested_attributes_for :content

after_save :update_settings_timestamp
after_validation :run_custom_validations

acts_as_url :name, url_attribute: :slug, scope: :parent_id, :only_when_blank => true

def build_content(params, assignment_options=nil)
self.content = content_type.constantize.new(params)
self.content = content_class.new(params)
end

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

##
Expand Down Expand Up @@ -192,8 +198,17 @@ def available?
!(self_and_ancestors.where(active: false).count > 0)
end

def custom_validators
content_class.try(:acts_as_node_configuration).try(:[], :validators)
end

private

def run_custom_validations
return if custom_validators.blank?
self.validates_with *custom_validators
end

def update_settings_timestamp
Settings['nodes.updated_at'] = Time.now
end
Expand Down Expand Up @@ -251,5 +266,6 @@ def common_field_options(key)
def common_field_default_value(key)
common_field_options(key)['default']
end

end
end
2 changes: 1 addition & 1 deletion app/views/releaf/content/_edit.body.html.haml
Expand Up @@ -11,7 +11,7 @@
.label_wrap
= f.label :content_type, t('template', scope: 'admin.global') + ':', :title => f.object.content_type
.value
%span= f.object.content_type
= f.text_field :content_type, disabled: :disabled
.clear


Expand Down
4 changes: 3 additions & 1 deletion lib/releaf/acts_as_node.rb
Expand Up @@ -17,7 +17,9 @@ def nodes
# There are no configuration options yet.
#
def acts_as_node(options = {})
configuration = {}
configuration = {
validators: [],
}
configuration.update(options) if options.is_a?(Hash)

ActsAsNode.register_class(self.name)
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/node.rb
Expand Up @@ -2,7 +2,7 @@
factory :node, class: ::Releaf::Node do
sequence(:name) {|n| "node #{n}"}
sequence(:slug) {|n| "node-#{n}"}
content_type "fake_type"
content_type "Text"
end

factory :text_node, class: ::Releaf::Node do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/acts_as_node_spec.rb
Expand Up @@ -17,7 +17,7 @@ class ContactFormController < ActionController::Base

describe ".acts_as_node" do
it "have configuration options available through acts_as_node_configuration class method" do
expect(Book.acts_as_node_configuration).to eq({:permit_attributes=>[:text_html]})
expect(Book.acts_as_node_configuration).to eq({validators: [], permit_attributes: [:text_html]})
end
end

Expand Down
65 changes: 65 additions & 0 deletions spec/models/node_spec.rb
Expand Up @@ -3,12 +3,45 @@
require "spec_helper"

describe Releaf::Node do
class TestValidation < ActiveModel::Validator
def validate record
end
end

class TestModel < ActiveRecord::Base
acts_as_node validators: [TestValidation]
end

class TestController < ActionController::Base
acts_as_node validators: [TestValidation]
end

let(:node) { Releaf::Node.new }

describe 'validations' do
it { should validate_presence_of(:name) }
it { should validate_presence_of(:slug) }
it { should validate_presence_of(:content_type) }

context "when content is model" do
context "when user suplied custom validations via acts_as_node" do
it "runs custom validations during validation" do
subject.content_type = 'TestModel'
expect_any_instance_of(TestValidation).to receive(:validate).with(subject)
subject.valid?
end
end
end

context "when content is controller" do
context "when user suplied custom validations via acts_as_node" do
it "runs custom validations during validation" do
subject.content_type = 'TestController'
expect_any_instance_of(TestValidation).to receive(:validate).with(subject)
subject.valid?
end
end
end
end

describe "after save" do
Expand Down Expand Up @@ -202,4 +235,36 @@
end
end
end

describe "#content_class" do
context "when #content_type is valid class name" do
it "returns constantizes content_class" do
subject.content_type = "String"
expect( subject.content_class ).to eq String
end
end

context "when #content_type is blank" do
it "returns nil" do
subject.content_type = ""
expect( subject.content_class ).to be_nil
end
end
end

describe "#custom_validators" do
context "when content_type is valid model name" do
it "returns user suplied validators via acts_as_node" do
subject.content_type = 'TestModel'
expect( subject.custom_validators ).to match_array [TestValidation]
end
end

context "when content_type is blank" do
it "returns nil" do
subject.content_type = ''
expect( subject.custom_validators ).to be_nil
end
end
end
end

0 comments on commit 296e37d

Please sign in to comment.