From d47bc11145ca6da7cf3c39a30aaf10c83dcf884f Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Thu, 28 Nov 2013 11:07:35 +0100 Subject: [PATCH] Test moderation of fields of an associated model --- spec/internal/app/models/category.rb | 1 + spec/internal/app/models/page.rb | 2 ++ spec/the_moderator/model_spec.rb | 34 ++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/spec/internal/app/models/category.rb b/spec/internal/app/models/category.rb index 747ea1b..1a65984 100644 --- a/spec/internal/app/models/category.rb +++ b/spec/internal/app/models/category.rb @@ -1,3 +1,4 @@ class Category < ActiveRecord::Base has_one :page + accepts_nested_attributes_for :page end diff --git a/spec/internal/app/models/page.rb b/spec/internal/app/models/page.rb index ff7769d..c0cd987 100644 --- a/spec/internal/app/models/page.rb +++ b/spec/internal/app/models/page.rb @@ -1,4 +1,6 @@ class Page < ActiveRecord::Base has_many :links + accepts_nested_attributes_for :links + belongs_to :category end diff --git a/spec/the_moderator/model_spec.rb b/spec/the_moderator/model_spec.rb index aefa92f..9532b27 100644 --- a/spec/the_moderator/model_spec.rb +++ b/spec/the_moderator/model_spec.rb @@ -1,24 +1,40 @@ require 'spec_helper' describe TheModerator::Model do - subject { Page.new(name: 'Name', content: 'Content') } - describe '#moderate' do it 'moderates simple field' do - subject.moderate(:name) + page = Page.new(name: 'Name', content: 'Content') + page.moderate(:name) - expect(subject.moderations).to have(1).moderation - expect(subject.name).to be_nil - expect(subject.content).to eq('Content') + expect(page.moderations).to have(1).moderation + expect(page.moderations.first.data[:attributes]).to include(name: 'Name') + expect(page.name).to be_nil + expect(page.content).to eq('Content') + end + + it 'moderates association fields' do + category = Category.new(name: 'category') + category.create_page + category.save + + category.attributes = {page_attributes: { + name: 'name', content: 'content', id: category.page.id}} + category.moderate(page: :name) + + expect(category.moderations).to have(1).moderation + expect(category.moderations.first.data[:attributes]) + .to include(page_attributes: {name: 'name', id: category.page.id}) + expect(category.page.name).to be_nil end end describe '#moderated?' do it 'detects moderated field' do - subject.moderate(:name) + page = Page.new(name: 'Name', content: 'Content') + page.moderate(:name) - expect(subject.moderated?(:name)).to be_true - expect(subject.moderated?(:content)).to be_false + expect(page.moderated?(:name)).to be_true + expect(page.moderated?(:content)).to be_false end end end