|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe ActiveAdmin::Namespace, "registering a resource" do |
| 4 | + |
| 5 | + let(:application){ ActiveAdmin::Application.new } |
| 6 | + |
| 7 | + let(:namespace){ ActiveAdmin::Namespace.new(application, :admin) } |
| 8 | + |
| 9 | + context "with no configuration" do |
| 10 | + before do |
| 11 | + namespace.register Category |
| 12 | + end |
| 13 | + it "should store the namespaced registered configuration" do |
| 14 | + namespace.resources.keys.should include('Category') |
| 15 | + end |
| 16 | + it "should create a new controller in the default namespace" do |
| 17 | + defined?(Admin::CategoriesController).should be_true |
| 18 | + end |
| 19 | + it "should create the dashboard controller" do |
| 20 | + defined?(Admin::DashboardController).should be_true |
| 21 | + end |
| 22 | + it "should create a menu item" do |
| 23 | + namespace.load_menu! |
| 24 | + namespace.menu["Categories"].should be_an_instance_of(ActiveAdmin::MenuItem) |
| 25 | + namespace.menu["Categories"].url.should == "/admin/categories" |
| 26 | + end |
| 27 | + end # context "with no configuration" |
| 28 | + |
| 29 | + context "with a block configuration" do |
| 30 | + it "should be evaluated in the dsl" do |
| 31 | + lambda { |
| 32 | + namespace.register Category do |
| 33 | + raise "Hello World" |
| 34 | + end |
| 35 | + }.should raise_error |
| 36 | + end |
| 37 | + end # context "with a block configuration" |
| 38 | + |
| 39 | + context "with a resource that's namespaced" do |
| 40 | + before do |
| 41 | + module ::Mock; class Resource; def self.has_many(arg1, arg2); end; end; end |
| 42 | + namespace.register Mock::Resource |
| 43 | + end |
| 44 | + |
| 45 | + it "should store the namespaced registered configuration" do |
| 46 | + namespace.resources.keys.should include('MockResource') |
| 47 | + end |
| 48 | + it "should create a new controller in the default namespace" do |
| 49 | + defined?(Admin::MockResourcesController).should be_true |
| 50 | + end |
| 51 | + it "should create a menu item" do |
| 52 | + namespace.load_menu! |
| 53 | + namespace.menu["Mock Resources"].should be_an_instance_of(ActiveAdmin::MenuItem) |
| 54 | + end |
| 55 | + it "should use the resource as the model in the controller" do |
| 56 | + Admin::MockResourcesController.resource_class.should == Mock::Resource |
| 57 | + end |
| 58 | + |
| 59 | + end # context "with a resource that's namespaced" |
| 60 | + |
| 61 | + describe "finding resource instances" do |
| 62 | + let(:namespace){ ActiveAdmin::Namespace.new(application, :admin) } |
| 63 | + |
| 64 | + it "should return the resource when its been registered" do |
| 65 | + post = namespace.register Post |
| 66 | + namespace.resource_for(Post).should == post |
| 67 | + end |
| 68 | + |
| 69 | + it 'should return nil when the resource has not been registered' do |
| 70 | + namespace.resource_for(Post).should == nil |
| 71 | + end |
| 72 | + |
| 73 | + it "should return the parent when the parent class has been registered and the child has not" do |
| 74 | + user = namespace.register User |
| 75 | + namespace.resource_for(Publisher).should == user |
| 76 | + end |
| 77 | + |
| 78 | + it "should return the resource if it and it's parent were registered" do |
| 79 | + user = namespace.register User |
| 80 | + publisher = namespace.register Publisher |
| 81 | + namespace.resource_for(Publisher).should == publisher |
| 82 | + end |
| 83 | + |
| 84 | + end # describe "finding resource instances" |
| 85 | + |
| 86 | + describe "adding to the menu" do |
| 87 | + describe "adding as a top level item" do |
| 88 | + before do |
| 89 | + namespace.register Category |
| 90 | + namespace.load_menu! |
| 91 | + end |
| 92 | + it "should add a new menu item" do |
| 93 | + namespace.menu['Categories'].should_not be_nil |
| 94 | + end |
| 95 | + end # describe "adding as a top level item" |
| 96 | + |
| 97 | + describe "adding as a child" do |
| 98 | + before do |
| 99 | + namespace.register Category do |
| 100 | + menu :parent => 'Blog' |
| 101 | + end |
| 102 | + namespace.load_menu! |
| 103 | + end |
| 104 | + it "should generate the parent menu item" do |
| 105 | + namespace.menu['Blog'].should_not be_nil |
| 106 | + end |
| 107 | + it "should generate its own child item" do |
| 108 | + namespace.menu['Blog']['Categories'].should_not be_nil |
| 109 | + end |
| 110 | + end # describe "adding as a child" |
| 111 | + |
| 112 | + describe "disabling the menu" do |
| 113 | + before do |
| 114 | + namespace.register Category do |
| 115 | + menu false |
| 116 | + end |
| 117 | + namespace.load_menu! |
| 118 | + end |
| 119 | + it "should not create a menu item" do |
| 120 | + namespace.menu["Categories"].should be_nil |
| 121 | + end |
| 122 | + end # describe "disabling the menu" |
| 123 | + |
| 124 | + describe "setting menu priority" do |
| 125 | + before do |
| 126 | + namespace.register Category do |
| 127 | + menu :priority => 2 |
| 128 | + end |
| 129 | + namespace.load_menu! |
| 130 | + end |
| 131 | + it "should have a custom priority of 2" do |
| 132 | + namespace.menu["Categories"].priority.should == 2 |
| 133 | + end |
| 134 | + end # describe "setting menu priority" |
| 135 | + |
| 136 | + describe "setting a condition for displaying" do |
| 137 | + before do |
| 138 | + namespace.register Category do |
| 139 | + menu :if => proc { false } |
| 140 | + end |
| 141 | + namespace.load_menu! |
| 142 | + end |
| 143 | + it "should have a proc returning false" do |
| 144 | + namespace.menu["Categories"].display_if_block.should be_instance_of(Proc) |
| 145 | + namespace.menu["Categories"].display_if_block.call.should == false |
| 146 | + end |
| 147 | + end # describe "setting a condition for displaying" |
| 148 | + |
| 149 | + describe "adding as a belongs to" do |
| 150 | + context "when not optional" do |
| 151 | + before do |
| 152 | + namespace.register Post do |
| 153 | + belongs_to :author |
| 154 | + end |
| 155 | + end |
| 156 | + it "should not show up in the menu" do |
| 157 | + namespace.menu["Posts"].should be_nil |
| 158 | + end |
| 159 | + end |
| 160 | + context "when optional" do |
| 161 | + before do |
| 162 | + namespace.register Post do |
| 163 | + belongs_to :author, :optional => true |
| 164 | + end |
| 165 | + end |
| 166 | + it "should show up in the menu" do |
| 167 | + namespace.menu["Posts"].should_not be_nil |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end # describe "adding to the menu" |
| 172 | + |
| 173 | + describe "dashboard controller name" do |
| 174 | + context "when namespaced" do |
| 175 | + it "should be namespaced" do |
| 176 | + namespace = ActiveAdmin::Namespace.new(application, :admin) |
| 177 | + namespace.dashboard_controller_name.should == "Admin::DashboardController" |
| 178 | + end |
| 179 | + end |
| 180 | + context "when not namespaced" do |
| 181 | + it "should not be namespaced" do |
| 182 | + namespace = ActiveAdmin::Namespace.new(application, :root) |
| 183 | + namespace.dashboard_controller_name.should == "DashboardController" |
| 184 | + end |
| 185 | + end |
| 186 | + end # describe "dashboard controller name" |
| 187 | +end # describe "registering a resource" |
0 commit comments