Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
2 parents 7dd0996 + 7cd7f44 commit 7151d7a74e1ef7c1aa920738d9e154d140b86f38 @damrbaby committed Dec 28, 2011
View
@@ -1,2 +1,3 @@
-default: --format 'progress' --require features/support/env.rb --require features/step_definitions features
-wip: --format 'progress' --require features/support/env.rb --require features/step_definitions features --tags @wip:3 --wip features
+default: --format 'progress' --require features/support/env.rb --require features/step_definitions features --tags ~@requires-reloading
+wip: --format 'progress' --require features/support/env.rb --require features/step_definitions features --tags @wip:3 --wip features
+class-reloading: RAILS_ENV=cucumber_with_reloading --format 'progress' --require features/support/env.rb --require features/step_definitions features --tags @requires-reloading
@@ -0,0 +1,27 @@
+Feature: Belongs To
+
+ A resource belongs to another resource
+
+ Background:
+ Given a configuration of:
+ """
+ ActiveAdmin.register User
+ ActiveAdmin.register Post do
+ belongs_to :user
+ end
+ """
+ And I am logged in
+ And a post with the title "Hello World" written by "John Doe" exists
+ And a post with the title "Hello World" written by "Jane Doe" exists
+
+ Scenario: Viewing the child resource index page
+ When I go to the last author's posts
+ Then the "Users" tab should be selected
+ And I should see "Displaying 1 Post"
+ And I should see a link to "Users" in the breadcrumb
+
+ Scenario: Viewing a child resource page
+ When I go to the last author's posts
+ And I follow "View"
+ Then I should be on the last author's last post page
+ And the "Users" tab should be selected
@@ -0,0 +1,19 @@
+Feature: Development Reloading
+
+ In order to quickly develop applications
+ As a developer
+ I want the application to reload itself in development
+
+ @requires-reloading
+ Scenario: Reloading an updated model that a resource points to
+ Given a configuration of:
+ """
+ ActiveAdmin.register Post
+ """
+ And I am logged in
+ And I create a new post with the title ""
+ Then I should see a successful create flash
+ Given I add "validates_presence_of :title" to the "post" model
+ And I create a new post with the title ""
+ Then I should not see a successful create flash
+ And I should see a validation error "can't be blank"
@@ -80,3 +80,6 @@
page.should have_css("#active_admin_content", :text => content)
end
+Then /^I should see a validation error "([^"]*)"$/ do |error_message|
+ page.should have_css(".inline-errors", :text => error_message)
+end
@@ -0,0 +1,5 @@
+Then /^I should see a link to "([^"]*)" in the breadcrumb$/ do |text|
+ within ".breadcrumb" do
+ page.should have_css("a", :text => text)
+ end
+end
@@ -9,8 +9,35 @@ def load_active_admin_configuration(configuration_content)
end
+module ActiveAdminContentsRollback
+
+ def self.recorded_files
+ @files ||= {}
+ end
+
+ # Records the contents of a file the first time we are
+ # about to change it
+ def self.record(filename)
+ recorded_files[filename] ||= File.read(filename)
+ end
+
+ # Rolls the recorded files back to their original states
+ def self.rollback!
+ recorded_files.each do |filename, contents|
+ File.open(filename, "w+") do |f|
+ f << contents
+ end
+ end
+ end
+
+end
+
World(ActiveAdminReloading)
+After do
+ ActiveAdminContentsRollback.rollback!
+end
+
Given /^a configuration of:$/ do |configuration_content|
load_active_admin_configuration(configuration_content)
ActiveAdmin.application.namespaces.values.each{|n| n.load_menu! }
@@ -37,3 +64,16 @@ def load_active_admin_configuration(configuration_content)
FileUtils.mkdir_p File.dirname(filepath)
File.open(filepath, 'w+'){|f| f << contents }
end
+
+Given /^I add "([^"]*)" to the "([^"]*)" model$/ do |code, model_name|
+ filename = File.join(Rails.root, "app", "models", "#{model_name}.rb")
+ ActiveAdminContentsRollback.record(filename)
+
+ # Update the file
+ contents = File.read(filename)
+ File.open(filename, "w+") do |f|
+ f << contents.gsub(/^(class .+)$/, "\\1\n #{code}\n")
+ end
+
+ ActiveSupport::Dependencies.clear
+end
@@ -27,3 +27,10 @@
type = type.camelize.constantize
type.create! :first_name => first, :last_name => last, :username => name
end
+
+Given /^I create a new post with the title "([^"]*)"$/ do |title|
+ click_link "Posts"
+ click_link "New Post"
+ fill_in :title, :with => title
+ click_button "Create Post"
+end
@@ -1,3 +1,11 @@
Then /^I should see a flash with "([^"]*)"$/ do |text|
- Then %{I should see "#{text}"}
+ page.should have_content(text)
+end
+
+Then /^I should see a successful create flash$/ do
+ page.should have_css('div.flash_notice', :text => /was successfully created/)
+end
+
+Then /^I should not see a successful create flash$/ do
+ page.should_not have_css('div.flash_notice', :text => /was successfully created/)
end
@@ -28,6 +28,12 @@ def path_to(page_name)
when /^the index page for (.*)$/
send(:"admin_#{$1}_path")
+ when /^the last author's posts$/
+ admin_user_posts_path(User.last)
+
+ when /^the last author's last post page$/
+ admin_user_post_path(User.last, Post.where(:author_id => User.last.id).last)
+
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
@@ -34,7 +34,10 @@ lv:
one: "<b>1</b> ieraksts"
one_page: "<b>%{n}</b> ieraksti"
multiple: "<b>%{from}&nbsp;-&nbsp;%{to}</b> ieraksti no <b>%{total}</b> kopā"
- any: "Any"
+ entry:
+ one: "ieraksts"
+ other: "ieraksti"
+ any: "Jebkurš"
blank_slate:
content: "Sadaļā '%{resource_name}' nav neviena ieraksta."
link: "Izveidot jaunu"
@@ -9,7 +9,9 @@
delete: "Remover"
delete_confirmation: "Você tem certeza que deseja remover este item?"
new_model: "Novo(a) %{model}"
+ create_model: "Novo(a) %{model}"
edit_model: "Editar %{model}"
+ update_model: "Atualizar %{model}"
delete_model: "Remover %{model}"
details: "Detalhes do(a) %{model}"
cancel: "Cancelar"
@@ -40,5 +42,4 @@
any: "Qualquer"
blank_slate:
content: "Não existem %{resource_name} ainda."
- link: "Crie uma"
-
+ link: "Crie uma"
@@ -25,9 +25,8 @@ class Resource
# The namespace this config belongs to
attr_reader :namespace
- # The class this resource wraps. If you register the Post model, Resource#resource
- # will point to the Post class
- attr_reader :resource_class
+ # The name of the resource class
+ attr_reader :resource_class_name
# An array of member actions defined for this resource
attr_reader :member_actions
@@ -50,7 +49,7 @@ class Resource
module Base
def initialize(namespace, resource_class, options = {})
@namespace = namespace
- @resource_class = resource_class
+ @resource_class_name = "::#{resource_class.name}"
@options = default_options.merge(options)
@sort_order = @options[:sort_order]
@member_actions, @collection_actions = [], []
@@ -66,6 +65,12 @@ def initialize(namespace, resource_class, options = {})
include Sidebars
include Menu
+ # The class this resource wraps. If you register the Post model, Resource#resource_class
+ # will point to the Post class
+ def resource_class
+ ActiveSupport::Dependencies.constantize(resource_class_name)
+ end
+
def resource_table_name
resource_class.quoted_table_name
end
@@ -5,6 +5,7 @@
require 'active_admin/resource_controller/collection'
require 'active_admin/resource_controller/filters'
require 'active_admin/resource_controller/scoping'
+require 'active_admin/resource_controller/resource_class_methods'
module ActiveAdmin
# All Resources Controller inherits from this controller.
@@ -23,15 +24,25 @@ class ResourceController < BaseController
include Collection
include Filters
include Scoping
+ extend ResourceClassMethods
class << self
def active_admin_config=(config)
@active_admin_config = config
+
defaults :resource_class => config.resource_class,
:route_prefix => config.route_prefix,
:instance_name => config.underscored_resource_name
end
+ # Inherited Resources uses the inherited(base) hook method to
+ # add in the Base.resource_class class method. To override it, we
+ # need to install our resource_class method each time we're inherited from.
+ def inherited(base)
+ super(base)
+ base.override_resource_class_methods!
+ end
+
public :belongs_to
end
@@ -0,0 +1,24 @@
+module ActiveAdmin
+ class ResourceController < BaseController
+ module ResourceClassMethods
+
+ # Override the default resource_class class and instance
+ # methods to only return the class defined in the instance
+ # of ActiveAdmin::Resource
+ def override_resource_class_methods!
+ self.class_eval do
+ def self.resource_class=(klass); end
+
+ def self.resource_class
+ @active_admin_config ? @active_admin_config.resource_class : nil
+ end
+
+ def resource_class
+ self.class.resource_class
+ end
+ end
+ end
+
+ end
+ end
+end
@@ -45,7 +45,7 @@ def empty_value
def content_for(attr_or_proc)
value = case attr_or_proc
when Proc
- attr_or_proc.call
+ attr_or_proc.call(@record)
else
content_for_attribute(attr_or_proc)
end
@@ -1,42 +0,0 @@
-require 'spec_helper'
-
-describe_with_capybara "Belongs To" do
-
- let(:user){ User.create(:first_name => "John", :last_name => "Doe", :username => "johndoe") }
- let(:post){ user.posts.create :title => "Hello World", :body => "woot!"}
-
- before do
- # Make sure both are created
- user
- post
- end
-
- describe "the index page" do
- before do
- visit admin_user_posts_path(user)
- end
-
- describe "the main content" do
- it "should display the default table" do
- page.should have_content(post.title)
- end
- end
-
- describe "the breadcrumb" do
- it "should have a link to the parent's index" do
- page.body.should have_tag("a", "Users", :attributes => { :href => "/admin/users" })
- end
- it "should have a link to the parent" do
- page.body.should have_tag("a", user.id.to_s, :attributes => { :href => "/admin/users/#{user.id}" })
- end
- end
-
- describe "the view links" do
- it "should take you to the sub resource" do
- click_link "View"
- current_path.should == "/admin/users/#{user.id}/posts/#{post.id}"
- end
- end
- end
-
-end
View
@@ -90,6 +90,10 @@ def mock_action_view(assigns = {})
end
alias_method :action_view, :mock_action_view
+ # A mock resource to register
+ class MockResource
+ end
+
end
ENV['RAILS_ENV'] = 'test'
@@ -2,8 +2,11 @@
# 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_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"
@@ -0,0 +1,5 @@
+require File.expand_path('config/environments/cucumber', Rails.root)
+
+Rails.application.class.configure do
+ config.cache_classes = false
+end
@@ -52,6 +52,7 @@ module ::Mock; class Resource; def self.has_many(arg1, arg2); end; end; end
namespace.load_menu!
namespace.menu["Mock Resources"].should be_an_instance_of(ActiveAdmin::MenuItem)
end
+
it "should use the resource as the model in the controller" do
Admin::MockResourcesController.resource_class.should == Mock::Resource
end
@@ -200,17 +200,15 @@ class ::News; def self.has_many(*); end end
context "when resource class responds to primary_key" do
it "should sort by primary key desc by default" do
- mock_resource = mock
- mock_resource.should_receive(:primary_key).and_return("pk")
- config = Resource.new(namespace, mock_resource)
+ MockResource.should_receive(:primary_key).and_return("pk")
+ config = Resource.new(namespace, MockResource)
config.sort_order.should == "pk_desc"
end
end
context "when resource class does not respond to primary_key" do
it "should default to id" do
- mock_resource = mock
- config = Resource.new(namespace, mock_resource)
+ config = Resource.new(namespace, MockResource)
config.sort_order.should == "id_desc"
end
end
Oops, something went wrong.

0 comments on commit 7151d7a

Please sign in to comment.