Nicolae Claudius
13 years ago
21 changed files with 376 additions and 25 deletions
@ -0,0 +1,32 @@
|
||||
class HostsController < ApplicationController |
||||
active_scaffold :a do |conf| |
||||
conf.columns = [:name, :host_domain, :content, :ttl, :change_date, :authentication_token] |
||||
conf.list.columns = [:name, :content, :ttl, :change_date, :authentication_token] |
||||
conf.create.columns = [:name, :host_domain, :content, :ttl,] |
||||
conf.update.columns = [:name, :host_domain, :content, :ttl] |
||||
conf.list.label = 'Hosts' |
||||
conf.create.link.label = "Add Host" |
||||
conf.columns[:host_domain].form_ui = :select |
||||
conf.columns[:host_domain].options = {:options => Settings.host_domains} |
||||
conf.columns[:name].label = 'Host' |
||||
conf.columns[:content].label = 'IP' |
||||
conf.columns[:content].description = 'Ex. "10.10.5.12"' |
||||
conf.columns[:change_date].list_ui = :timestamp |
||||
conf.columns[:ttl].options = {:i18n_number => {:delimiter => ''}} |
||||
conf.actions.exclude :show |
||||
end |
||||
|
||||
protected |
||||
|
||||
def new_model |
||||
record = super |
||||
record.content = client_remote_ip |
||||
before_create_save(record) |
||||
record |
||||
end |
||||
|
||||
def before_create_save(record) |
||||
record.user = current_user |
||||
end |
||||
|
||||
end |
@ -1,27 +1,24 @@
|
||||
class Ability |
||||
include CanCan::Ability |
||||
attr_accessor :user |
||||
attr_accessor :context |
||||
|
||||
def initialize(user) |
||||
|
||||
user ||= User.new |
||||
def initialize(options) |
||||
@user = options[:user] || User.new |
||||
@context = options[:context] || :application |
||||
|
||||
if user.persisted? |
||||
|
||||
# can manage his domains and records |
||||
can :manage, Domain, :user_id => user.id |
||||
can :manage, Record, :domain => {:user_id => user.id} |
||||
cannot :delete, SOA # it's deleted with the parent domain |
||||
|
||||
# can manage his hosts |
||||
can :manage, A, :user_id => user.id #, :domain => {:name => Settings.host_domains} |
||||
|
||||
end |
||||
|
||||
# The first argument to `can` is the action you are giving the user permission to do. |
||||
# If you pass :manage it will apply to every action. Other common actions here are |
||||
# :read, :create, :update and :destroy. |
||||
# |
||||
# The second argument is the resource the user can perform the action on. If you pass |
||||
# :all it will apply to every resource. Otherwise pass a Ruby class of the resource. |
||||
# |
||||
# The third argument is an optional hash of conditions to further filter the objects. |
||||
# For example, here the user can only update published articles. |
||||
# |
||||
# can :update, Article, :published => true |
||||
# |
||||
# See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities |
||||
end |
||||
end |
||||
|
@ -0,0 +1,5 @@
|
||||
class AddUserIdToRecords < ActiveRecord::Migration |
||||
def change |
||||
add_column :records, :user_id, :integer |
||||
end |
||||
end |
@ -0,0 +1,157 @@
|
||||
require 'spec_helper' |
||||
|
||||
# This spec was generated by rspec-rails when you ran the scaffold generator. |
||||
# It demonstrates how one might use RSpec to specify the controller code that |
||||
# was generated by Rails when you ran the scaffold generator. |
||||
# |
||||
# It assumes that the implementation code is generated by the rails scaffold |
||||
# generator. If you are using any extension libraries to generate different |
||||
# controller code, this generated spec may or may not pass. |
||||
# |
||||
# It only uses APIs available in rails and/or rspec-rails. There are a number |
||||
# of tools you can use to make these specs even more expressive, but we're |
||||
# sticking to rails and rspec-rails APIs to keep things simple and stable. |
||||
# |
||||
# Compared to earlier versions of this generator, there is very limited use of |
||||
# stubs and message expectations in this spec. Stubs are only used when there |
||||
# is no simpler way to get a handle on the object needed for the example. |
||||
# Message expectations are only used when there is no simpler way to specify |
||||
# that an instance is receiving a specific message. |
||||
|
||||
describe HostsController do |
||||
|
||||
# This should return the minimal set of attributes required to create a valid |
||||
# Host. As you add validations to Host, be sure to |
||||
# update the return value of this method accordingly. |
||||
def valid_attributes |
||||
{} |
||||
end |
||||
|
||||
describe "GET index" do |
||||
it "assigns all hosts as @hosts" do |
||||
host = Host.create! valid_attributes |
||||
get :index |
||||
assigns(:hosts).should eq([host]) |
||||
end |
||||
end |
||||
|
||||
describe "GET show" do |
||||
it "assigns the requested host as @host" do |
||||
host = Host.create! valid_attributes |
||||
get :show, :id => host.id.to_s |
||||
assigns(:host).should eq(host) |
||||
end |
||||
end |
||||
|
||||
describe "GET new" do |
||||
it "assigns a new host as @host" do |
||||
get :new |
||||
assigns(:host).should be_a_new(Host) |
||||
end |
||||
end |
||||
|
||||
describe "GET edit" do |
||||
it "assigns the requested host as @host" do |
||||
host = Host.create! valid_attributes |
||||
get :edit, :id => host.id.to_s |
||||
assigns(:host).should eq(host) |
||||
end |
||||
end |
||||
|
||||
describe "POST create" do |
||||
describe "with valid params" do |
||||
it "creates a new Host" do |
||||
expect { |
||||
post :create, :host => valid_attributes |
||||
}.to change(Host, :count).by(1) |
||||
end |
||||
|
||||
it "assigns a newly created host as @host" do |
||||
post :create, :host => valid_attributes |
||||
assigns(:host).should be_a(Host) |
||||
assigns(:host).should be_persisted |
||||
end |
||||
|
||||
it "redirects to the created host" do |
||||
post :create, :host => valid_attributes |
||||
response.should redirect_to(Host.last) |
||||
end |
||||
end |
||||
|
||||
describe "with invalid params" do |
||||
it "assigns a newly created but unsaved host as @host" do |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Host.any_instance.stub(:save).and_return(false) |
||||
post :create, :host => {} |
||||
assigns(:host).should be_a_new(Host) |
||||
end |
||||
|
||||
it "re-renders the 'new' template" do |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Host.any_instance.stub(:save).and_return(false) |
||||
post :create, :host => {} |
||||
response.should render_template("new") |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "PUT update" do |
||||
describe "with valid params" do |
||||
it "updates the requested host" do |
||||
host = Host.create! valid_attributes |
||||
# Assuming there are no other hosts in the database, this |
||||
# specifies that the Host created on the previous line |
||||
# receives the :update_attributes message with whatever params are |
||||
# submitted in the request. |
||||
Host.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) |
||||
put :update, :id => host.id, :host => {'these' => 'params'} |
||||
end |
||||
|
||||
it "assigns the requested host as @host" do |
||||
host = Host.create! valid_attributes |
||||
put :update, :id => host.id, :host => valid_attributes |
||||
assigns(:host).should eq(host) |
||||
end |
||||
|
||||
it "redirects to the host" do |
||||
host = Host.create! valid_attributes |
||||
put :update, :id => host.id, :host => valid_attributes |
||||
response.should redirect_to(host) |
||||
end |
||||
end |
||||
|
||||
describe "with invalid params" do |
||||
it "assigns the host as @host" do |
||||
host = Host.create! valid_attributes |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Host.any_instance.stub(:save).and_return(false) |
||||
put :update, :id => host.id.to_s, :host => {} |
||||
assigns(:host).should eq(host) |
||||
end |
||||
|
||||
it "re-renders the 'edit' template" do |
||||
host = Host.create! valid_attributes |
||||
# Trigger the behavior that occurs when invalid params are submitted |
||||
Host.any_instance.stub(:save).and_return(false) |
||||
put :update, :id => host.id.to_s, :host => {} |
||||
response.should render_template("edit") |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "DELETE destroy" do |
||||
it "destroys the requested host" do |
||||
host = Host.create! valid_attributes |
||||
expect { |
||||
delete :destroy, :id => host.id.to_s |
||||
}.to change(Host, :count).by(-1) |
||||
end |
||||
|
||||
it "redirects to the hosts list" do |
||||
host = Host.create! valid_attributes |
||||
delete :destroy, :id => host.id.to_s |
||||
response.should redirect_to(hosts_url) |
||||
end |
||||
end |
||||
|
||||
end |
@ -0,0 +1,15 @@
|
||||
require 'spec_helper' |
||||
|
||||
# Specs in this file have access to a helper object that includes |
||||
# the HostsHelper. For example: |
||||
# |
||||
# describe HostsHelper do |
||||
# describe "string concat" do |
||||
# it "concats two strings with spaces" do |
||||
# helper.concat_strings("this","that").should == "this that" |
||||
# end |
||||
# end |
||||
# end |
||||
describe HostsHelper do |
||||
pending "add some examples to (or delete) #{__FILE__}" |
||||
end |
@ -0,0 +1,11 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "Hosts" do |
||||
describe "GET /hosts" do |
||||
it "works! (now write some real specs)" do |
||||
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers |
||||
get hosts_path |
||||
response.status.should be(200) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,35 @@
|
||||
require "spec_helper" |
||||
|
||||
describe HostsController do |
||||
describe "routing" do |
||||
|
||||
it "routes to #index" do |
||||
get("/hosts").should route_to("hosts#index") |
||||
end |
||||
|
||||
it "routes to #new" do |
||||
get("/hosts/new").should route_to("hosts#new") |
||||
end |
||||
|
||||
it "routes to #show" do |
||||
get("/hosts/1").should route_to("hosts#show", :id => "1") |
||||
end |
||||
|
||||
it "routes to #edit" do |
||||
get("/hosts/1/edit").should route_to("hosts#edit", :id => "1") |
||||
end |
||||
|
||||
it "routes to #create" do |
||||
post("/hosts").should route_to("hosts#create") |
||||
end |
||||
|
||||
it "routes to #update" do |
||||
put("/hosts/1").should route_to("hosts#update", :id => "1") |
||||
end |
||||
|
||||
it "routes to #destroy" do |
||||
delete("/hosts/1").should route_to("hosts#destroy", :id => "1") |
||||
end |
||||
|
||||
end |
||||
end |
@ -0,0 +1,15 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "hosts/edit.html.erb" do |
||||
before(:each) do |
||||
@host = assign(:host, stub_model(Host)) |
||||
end |
||||
|
||||
it "renders the edit host form" do |
||||
render |
||||
|
||||
# Run the generator again with the --webrat flag if you want to use webrat matchers |
||||
assert_select "form", :action => hosts_path(@host), :method => "post" do |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,14 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "hosts/index.html.erb" do |
||||
before(:each) do |
||||
assign(:hosts, [ |
||||
stub_model(Host), |
||||
stub_model(Host) |
||||
]) |
||||
end |
||||
|
||||
it "renders a list of hosts" do |
||||
render |
||||
end |
||||
end |
@ -0,0 +1,15 @@
|
||||
require 'spec_helper' |
||||
|
||||
describe "hosts/new.html.erb" do |
||||
before(:each) do |
||||
assign(:host, stub_model(Host).as_new_record) |
||||
end |
||||
|
||||
it "renders new host form" do |
||||
render |
||||
|
||||
# Run the generator again with the --webrat flag if you want to use webrat matchers |
||||
assert_select "form", :action => hosts_path, :method => "post" do |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue