diff --git a/features/engine/has_many.feature b/features/engine/has_many.feature new file mode 100644 index 00000000..69016d46 --- /dev/null +++ b/features/engine/has_many.feature @@ -0,0 +1,11 @@ +Feature: Has Many Association + As a designer + In order to make dealing with models easier + I want to be able to display other models that have a has many association + + Background: + Given I have the site: "test site" set up + + Scenario: Paginating a has many association + Given I have an "Articles" model which has many "Comments" + Then I should be able to view a paginated list of "Comments" per "Articles" diff --git a/features/step_definitions/content_types_steps.rb b/features/step_definitions/content_types_steps.rb index 6e65375b..d51255c4 100644 --- a/features/step_definitions/content_types_steps.rb +++ b/features/step_definitions/content_types_steps.rb @@ -66,4 +66,3 @@ end Then %r{^I should see once the "([^"]*)" field$} do |field| page.should have_css("#content_#{field.underscore.downcase}_input", :count => 1) end - diff --git a/features/step_definitions/has_many_steps.rb b/features/step_definitions/has_many_steps.rb new file mode 100644 index 00000000..c2b30a78 --- /dev/null +++ b/features/step_definitions/has_many_steps.rb @@ -0,0 +1,47 @@ +Given %r{^I have an? "([^"]*)" model which has many "([^"]*)"$} do |parent_model, child_model| + @parent_model = FactoryGirl.build(:content_type, :site => @site, :name => parent_model).tap do |ct| + ct.content_custom_fields.build :label => 'Body', :kind => 'string', :required => false + ct.save! + end + @child_model = FactoryGirl.build(:content_type, :site => @site, :name => child_model).tap do |ct| + ct.content_custom_fields.build :label => 'Body', :kind => 'string', :required => false + ct.content_custom_fields.build :label => parent_model.singularize, :kind => 'has_one', :required => false, :target => parent_model + ct.save! + end + + @parent_model.content_custom_fields.build({ + :label => child_model, + :kind => 'has_many', + :target => @child_model.content_klass.to_s, + :reverse_lookup => @child_model.content_klass.custom_field_alias_to_name(parent_model.downcase.singularize) + }) +end + +Then /^I should be able to view a paginated list of "([^"]*)" per "([^"]*)"$/ do |parent_model, child_model| + # Create contents + @parent_model.contents.create!(:slug => 'parent', :body => 'Parent') + @child_model.contents.create!(:slug => 'one', :body => 'One') + @child_model.contents.create!(:slug => 'two', :body => 'Two') + @child_model.contents.create!(:slug => 'three', :body => 'Three') + + # Create a page + raw_template = %{ + {% for article in contents.articles %} + {% paginate article.comments by 2 %} + {% for comment in paginate.collection %} + {{ comment.body }} + {% endfor %} + {{ paginate | default_pagination }} + {% endpaginate %} + {% endfor %} + } + + # Create a page + FactoryGirl.create(:page, :site => @site, :slug => 'hello', :raw_template => raw_template) + + # The page should have the first two comments + visit '/hello' + page.should have_content 'one' + page.should have_content 'two' + page.should_not have_content 'three' +end diff --git a/spec/factories.rb b/spec/factories.rb index 22eaf582..3968ccb9 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -138,4 +138,7 @@ FactoryGirl.define do site { Site.where(:subdomain => "acme").first || Factory(:site) } end -end \ No newline at end of file + factory :content_instance do + end + +end