Skip to content

Commit

Permalink
batch actions cuke step refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlinsley committed Apr 11, 2013
1 parent d6a8c51 commit e63b9fe
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 128 deletions.
10 changes: 5 additions & 5 deletions features/index/batch_actions.feature
Expand Up @@ -67,7 +67,7 @@ Feature: Batch Actions
config.batch_actions = false
end
"""
Then I should not see the batch actions selector
Then I should not see the batch action selector
And I should not see checkboxes in the table

Scenario: Disabling the default destroy batch action
Expand All @@ -79,8 +79,8 @@ Feature: Batch Actions
batch_action(:flag) {}
end
"""
Then I should see the batch action "Flag Selected"
And I should not see the batch action "Delete Selected"
Then I should see the batch action :flag "Flag Selected"
And I should not see the batch action :destroy "Delete Selected"

Scenario: Optional display of batch actions
Given 10 posts exist
Expand All @@ -91,8 +91,8 @@ Feature: Batch Actions
batch_action(:unflag, :if => proc { false }) {}
end
"""
Then I should see the batch action "Flag Selected"
And I should not see the batch action "Unflag Selected"
Then I should see the batch action :flag "Flag Selected"
And I should not see the batch action :unflag "Unflag Selected"

Scenario: Sort order priority
Given 10 posts exist
Expand Down
6 changes: 3 additions & 3 deletions features/index/filters.feature
Expand Up @@ -66,7 +66,7 @@ Feature: Index Filtering
When I press "Filter"
Then I should see 2 posts in the table
And I should see "Hello World" within ".index_table"
And the "jane_doe" checkbox should not be checked
And the "Jane Doe" checkbox should not be checked

Scenario: Checkboxes - Filtering posts written by Jane Doe
Given 1 post exists
Expand All @@ -77,11 +77,11 @@ Feature: Index Filtering
filter :author, :as => :check_boxes
end
"""
When I check "jane_doe"
When I check "Jane Doe"
And I press "Filter"
Then I should see 1 posts in the table
And I should see "Hello World" within ".index_table"
And the "jane_doe" checkbox should be checked
And the "Jane Doe" checkbox should be checked

Scenario: Disabling filters
Given an index configuration of:
Expand Down
4 changes: 2 additions & 2 deletions features/index/index_scopes.feature
Expand Up @@ -122,7 +122,7 @@ Feature: Index Scoping
And I should see the scope "Published" with the count 3
And I should see 3 posts in the table

When I select "daft_punk" from "Author"
When I select "Daft Punk" from "Author"
And I press "Filter"

Then I should see the scope "Published" selected
Expand Down Expand Up @@ -242,7 +242,7 @@ Feature: Index Scoping
And I should see the scope "Published" with the count 1
And I should see 1 posts in the table

When I select "daft_punk" from "Author"
When I select "Daft Punk" from "Author"
And I press "Filter"

Then I should see the scope "Published" selected
Expand Down
6 changes: 3 additions & 3 deletions features/show/default_content.feature
Expand Up @@ -13,7 +13,7 @@ Feature: Show - Default Content
Then I should see the attribute "Title" with "Hello World"
And I should see the attribute "Body" with "Empty"
And I should see the attribute "Created At" with a nicely formatted datetime
And I should see the attribute "Author" with "jane_doe"
And I should see the attribute "Author" with "Jane Doe"
And I should see an action item button "Delete Post"
And I should see an action item button "Edit Post"

Expand All @@ -23,8 +23,8 @@ Feature: Show - Default Content
ActiveAdmin.register Post
ActiveAdmin.register User
"""
Then I should see the attribute "Author" with "jane_doe"
And I should see a link to "jane_doe"
Then I should see the attribute "Author" with "Jane Doe"
And I should see a link to "Jane Doe"

Scenario: Customizing the attributes table with a set of attributes
Given a show configuration of:
Expand Down
100 changes: 25 additions & 75 deletions features/step_definitions/batch_action_steps.rb
@@ -1,105 +1,55 @@
# TODO
Then /^I (should|should not) see the batch action "([^"]*)"$/ do |maybe, title|
%{Then I #{maybe} see the batch action :#{title.gsub(' ','').gsub(" Selected", "").underscore} "#{title}"}
end

Then /^I (should|should not) be asked to confirm "([^"]*)" for "([^"]*)"$/ do |maybe, confirmation, title|
within "#batch_actions_popover" do
unless maybe == "should not"
link = page.find "a.batch_action", :text => title
link["data-confirm"].should match( confirmation )
else
page.should_not have_css("a.batch_action", :text => title)
end
end
selector = "#batch_actions_popover a.batch_action:contains('#{title}')"
selector << "[data-confirm='#{confirmation}']" if maybe == 'should'
page.send maybe.sub(' ', '_'), have_css(selector)
end

Then /^I (should|should not) see the batch action :([^\s]*) "([^"]*)"$/ do |maybe, sym, title|
within "#batch_actions_selector" do
unless maybe == "should not"
link = page.find "a.batch_action", :text => title
link["data-action"].should match( sym )
link[:href].should match( "#" )
else
page.should_not have_css("a.batch_action", :text => title)
end
end
selector = "#batch_actions_selector a.batch_action:contains('#{title}')"
selector << "[href='#'][data-action='#{sym}']" if maybe == 'should'
page.send maybe.sub(' ', '_'), have_css(selector)
end

Then /^the (\d+)(?:st|nd|rd|th) batch action should be "([^"]*)"$/ do |index, title|
within "#batch_actions_selector" do
page.all( "a.batch_action" )[index.to_i - 1].text.should match( title )
end
page.all("#batch_actions_selector a.batch_action")[index.to_i - 1].text.should match title
end

When /^I check the (\d+)(?:st|nd|rd|th) record$/ do |index|
page.all( "table.index_table input[type=checkbox]" )[index.to_i].set( true )
end

When /^I uncheck the (\d+)(?:st|nd|rd|th) record$/ do |index|
page.all( "table.index_table input[type='checkbox']" )[index.to_i].set( false )
page.all("table.index_table input[type=checkbox]")[index.to_i].set true
end

When /^I toggle the collection selection$/ do
toggle_box = page.find( "#collection_selection_toggle_all" )
toggle_box.click
end

Then /^I should see (\d+) record(?:s)? selected$/ do |count|
within "table.index_table" do
unless count.to_i == 0
page.should have_xpath(".//input[@type='checkbox' and @checked='checked']", :count => count)
else
page.should have_no_xpath(".//input[@type='checkbox' and @checked='checked']")
end
end
page.find("#collection_selection_toggle_all").click
end

Then /^I should see that the batch action button is disabled$/ do
page.should have_css("#batch_actions_selector .dropdown_menu_button.disabled")
end

Then /^I (should|should not) see the batch action button$/ do |maybe|
if maybe == "should not"
page.should_not have_css("div.table_tools #batch_actions_selector .dropdown_menu_button")
else
page.should have_css("div.table_tools #batch_actions_selector .dropdown_menu_button")
end
page.should have_css "#batch_actions_selector .dropdown_menu_button.disabled"
end

Then "I should not see the batch actions selector" do
page.should_not have_css("div.table_tools #batch_actions_selector")
Then /^I (should|should not) see the batch action (button|selector)$/ do |maybe, type|
selector = "div.table_tools #batch_actions_selector"
selector << ' .dropdown_menu_button' if maybe == 'should' && type == 'button'
page.send maybe.sub(' ', '_'), have_css(selector)
end

Then /^I should see the batch action popover exists$/ do
page.should have_css("#batch_actions_selector")
page.should have_css "#batch_actions_selector"
end

Given /^I submit the batch action form with "([^"]*)"$/ do |action|
page.find(:css, "#batch_action").set(action)

within("#main_content") do
@params = page.all("input").reduce({}) do |acc, input|
param_key = input['name']
param_value = input['value']

if param_key == 'collection_selection[]'
# the collection_selection should be sent as an array containing only IDs of checked rows
acc[param_key] ||= Array.new
acc[param_key] << param_value if input.checked?
else
# other inputs, just send the value normally
acc.store(param_key, param_value)
end
acc
page.find("#batch_action").set action
form = page.find "#collection_selection"
params = page.all("#main_content input").each_with_object({}) do |input, obj|
key, value = input['name'], input['value']
if key == 'collection_selection[]'
(obj[key] ||= []).push value if input.checked?
else
obj[key] = value
end
end

form = page.find("#collection_selection")

page.driver.submit(form['method'].to_sym, form['action'], @params)
page.driver.submit form['method'], form['action'], params
end

Then /^I should not see checkboxes in the table$/ do
page.should_not have_css( ".paginated_collection table input[type=checkbox]" )
page.should_not have_css ".paginated_collection table input[type=checkbox]"
end
46 changes: 26 additions & 20 deletions spec/support/rails_template.rb
@@ -1,42 +1,50 @@
# Rails template to build the sample app for specs

# Create a cucumber database and environment
copy_file File.expand_path('../templates/cucumber.rb', __FILE__), "config/environments/cucumber.rb"
copy_file File.expand_path('../templates/cucumber.rb', __FILE__), "config/environments/cucumber.rb"
copy_file File.expand_path('../templates/cucumber_with_reloading.rb', __FILE__), "config/environments/cucumber_with_reloading.rb"

gsub_file 'config/database.yml', /^test:.*\n/, "test: &test\n"
gsub_file 'config/database.yml', /\z/, "\ncucumber:\n <<: *test\n database: db/cucumber.sqlite3"
gsub_file 'config/database.yml', /\z/, "\ncucumber_with_reloading:\n <<: *test\n database: db/cucumber.sqlite3"

# Generate some test models
generate :model, "post title:string body:text published_at:datetime author_id:integer category_id:integer starred:boolean"
inject_into_file 'app/models/post.rb', " belongs_to :author, :class_name => 'User'\n belongs_to :category\n accepts_nested_attributes_for :author\n", :after => "class Post < ActiveRecord::Base\n"

# We'll put this basic delegator in app/models in order to simplify auto-loading.
inject_into_file 'app/models/post.rb', %q{
belongs_to :category
belongs_to :author, :class_name => 'User'
accepts_nested_attributes_for :author
attr_accessible :author if Rails::VERSION::STRING >= '3.2'
}, :after => 'class Post < ActiveRecord::Base'
copy_file File.expand_path('../templates/post_decorator.rb', __FILE__), "app/models/post_decorator.rb"

# Rails 3.2.3 model generator declare attr_accessible
inject_into_file 'app/models/post.rb', " attr_accessible :author\n", :before => "end" if Rails::VERSION::STRING >= '3.2'
generate :model, "user type:string first_name:string last_name:string username:string age:integer"
inject_into_file 'app/models/user.rb', " has_many :posts, :foreign_key => 'author_id'\n", :after => "class User < ActiveRecord::Base\n"
generate :model, "publisher --migration=false --parent=User"
inject_into_file 'app/models/user.rb', %q{
has_many :posts, :foreign_key => 'author_id'
def display_name
"#{first_name} #{last_name}"
end
}, :after => 'class User < ActiveRecord::Base'

generate :model, 'publisher --migration=false --parent=User'
generate :model, 'category name:string description:text'
inject_into_file 'app/models/category.rb', " has_many :posts\n accepts_nested_attributes_for :posts\n", :after => "class Category < ActiveRecord::Base\n"
inject_into_file 'app/models/category.rb', %q{
has_many :posts
accepts_nested_attributes_for :posts
}, :after => 'class Category < ActiveRecord::Base'
generate :model, 'store name:string'

# Generate a model with string ids
generate :model, "tag name:string"
gsub_file(Dir['db/migrate/*_create_tags.rb'][0], /\:tags\sdo\s.*/, ":tags, :id => false, :primary_key => :id do |t|\n\t\t\tt.string :id\n")
id_model_setup = <<-EOF
inject_into_file 'app/models/tag.rb', %q{
self.primary_key = :id
before_create :set_id
private
def set_id
self.id = 8.times.inject("") { |s,e| s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
end
EOF
inject_into_file 'app/models/tag.rb', id_model_setup, :after => "class Tag < ActiveRecord::Base\n"
}, :after => 'class Tag < ActiveRecord::Base'

if Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 1 #Rails 3.1 Gotcha
gsub_file 'app/models/tag.rb', /self\.primary_key.*$/, "define_attr_method :primary_key, :id"
Expand All @@ -61,11 +69,11 @@ def set_id
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

# we need this routing path, named "logout_path", for testing
route <<-EOS
route %q{
devise_scope :user do
match '/admin/logout' => 'active_admin/devise/sessions#destroy', :as => :logout
end
EOS
}

generate :'active_admin:install'

Expand All @@ -88,8 +96,7 @@ def setup_parallel_tests_database(after, force_insert_same_content = false)
# Somehow, calling parallel_tests tasks from Rails generator using Thor does not work ...
# RAILS_ENV variable never makes it to parallel_tests tasks.
# We need to call these tasks in the after set up hook in order to creates cucumber DBs + run migrations on test & cucumber DBs
create_file 'lib/tasks/parallel.rake' do
<<'RAKE'
create_file 'lib/tasks/parallel.rake', %q{
namespace :parallel do
def run_in_parallel(cmd, options)
count = "-n #{options[:count]}" if options[:count]
Expand All @@ -108,5 +115,4 @@ def run_in_parallel(cmd, options)
run_in_parallel("rake db:schema:load RAILS_ENV=cucumber", args)
end
end
RAKE
end
}
27 changes: 9 additions & 18 deletions spec/unit/filters/filter_form_builder_spec.rb
@@ -1,5 +1,4 @@
require 'spec_helper'

require 'spec_helper'

describe ActiveAdmin::Filters::ViewHelper do

Expand Down Expand Up @@ -170,35 +169,27 @@ def filter(name, options = {})
let(:body) { filter :author_id }

it "should not render as an integer" do
body.should_not have_tag("input", :attributes => {
:name => "q[author_id_eq]"})
body.should_not have_tag "input", :attributes => { :name => "q[author_id_eq]" }
end
it "should render as belongs to select" do
body.should have_tag("select", :attributes => {
:name => "q[author_id_eq]"})
body.should have_tag("option", "john_doe", :attributes => {
:value => @john.id })
body.should have_tag("option", "jane_doe", :attributes => {
:value => @jane.id })
body.should have_tag "select", :attributes => { :name => "q[author_id_eq]" }
body.should have_tag "option", "John Doe", :attributes => { :value => @john.id }
body.should have_tag "option", "Jane Doe", :attributes => { :value => @jane.id }
end
end

context "when given as the name of the relationship" do
let(:body) { filter :author }

it "should generate a select" do
body.should have_tag("select", :attributes => {
:name => "q[author_id_eq]"})
body.should have_tag "select", :attributes => { :name => "q[author_id_eq]" }
end
it "should set the default text to 'Any'" do
body.should have_tag("option", "Any", :attributes => {
:value => "" })
body.should have_tag "option", "Any", :attributes => { :value => "" }
end
it "should create an option for each related object" do
body.should have_tag("option", "john_doe", :attributes => {
:value => @john.id })
body.should have_tag("option", "jane_doe", :attributes => {
:value => @jane.id })
body.should have_tag "option", "John Doe", :attributes => { :value => @john.id }
body.should have_tag "option", "Jane Doe", :attributes => { :value => @jane.id }
end

context "with a proc" do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/views/components/attributes_table_spec.rb
Expand Up @@ -111,11 +111,11 @@
end

it "should check if an association exists when an attribute has id in it" do
post.author = User.new(:username => "john_doe")
post.author = User.new :username => 'john_doe', :first_name => 'John', :last_name => 'Doe'
table = render_arbre_component(assigns) {
attributes_table_for post, :author_id
}
table.find_by_tag("td").first.content.should == "john_doe"
table.find_by_tag('td').first.content.should == 'John Doe'
end

end
Expand Down

1 comment on commit e63b9fe

@seanlinsley
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI for anyone that's running into failing tests as of this commit, you need to remove the old rails test app so it can be recreated:

rm -r spec/rails

Please sign in to comment.