From cc721ab069effc7c1709769a82c280d6f81c69c2 Mon Sep 17 00:00:00 2001 From: grosser Date: Sun, 13 Sep 2009 13:37:47 +0200 Subject: [PATCH] command and multple view changes --- app/controllers/commands_controller.rb | 2 ++ app/controllers/home_controller.rb | 2 -- app/helpers/application_helper.rb | 14 ++++++++- app/models/command.rb | 4 +++ app/models/user.rb | 4 ++- app/views/commands/_command.erb | 3 ++ app/views/commands/index.erb | 1 + app/views/home/index.erb | 1 - app/views/layouts/_menu.erb | 1 + app/views/layouts/application.erb | 8 +++-- config/routes.rb | 3 +- db/migrate/20090913103817_create_commands.rb | 17 +++++++++++ db/schema.rb | 12 +++++++- spec/factories.rb | 5 +++ spec/helpers/application_helper_spec.rb | 32 ++++++++++++++++++++ spec/models/user_spec.rb | 10 ++++++ 16 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 app/controllers/commands_controller.rb delete mode 100644 app/controllers/home_controller.rb create mode 100644 app/models/command.rb create mode 100644 app/views/commands/_command.erb create mode 100644 app/views/commands/index.erb delete mode 100644 app/views/home/index.erb create mode 100644 app/views/layouts/_menu.erb create mode 100644 db/migrate/20090913103817_create_commands.rb create mode 100644 spec/helpers/application_helper_spec.rb diff --git a/app/controllers/commands_controller.rb b/app/controllers/commands_controller.rb new file mode 100644 index 0000000..5037898 --- /dev/null +++ b/app/controllers/commands_controller.rb @@ -0,0 +1,2 @@ +class CommandsController < RestfulController +end \ No newline at end of file diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb deleted file mode 100644 index 186a59c..0000000 --- a/app/controllers/home_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class HomeController < ApplicationController -end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..dfc717f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,14 @@ module ApplicationHelper -end + def title + return @title if @title + if @current_object + if @current_object.new_record? + "New #{current_model}" + else + @current_object.to_s + end + else + current_model.to_s.pluralize + end + end +end \ No newline at end of file diff --git a/app/models/command.rb b/app/models/command.rb new file mode 100644 index 0000000..694448d --- /dev/null +++ b/app/models/command.rb @@ -0,0 +1,4 @@ +class Command < ActiveRecord::Base + validates_uniqueness_of :name, :case_sensitive => false + validates_presence_of :name, :description +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 83842ad..8db9e0c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,7 @@ def can_create?(object) return true if admin? case object when User then anonymouse? + when Command then false else raise "NOT SUPPORTED" end end @@ -26,6 +27,7 @@ def can_write?(object) return true if admin? case object when User then object == self + when Command then false else raise "NOT SUPPORTED" end end @@ -33,7 +35,7 @@ def can_write?(object) def can_read?(object) return true if admin? case object - when User then true + when User, Command then true else raise "NOT SUPPORTED" end end diff --git a/app/views/commands/_command.erb b/app/views/commands/_command.erb new file mode 100644 index 0000000..de058d2 --- /dev/null +++ b/app/views/commands/_command.erb @@ -0,0 +1,3 @@ +

<%= command.name %>

+<%= command.description %>
+<%= command.mac_only? ? "Mac only" : "" %> \ No newline at end of file diff --git a/app/views/commands/index.erb b/app/views/commands/index.erb new file mode 100644 index 0000000..2d42026 --- /dev/null +++ b/app/views/commands/index.erb @@ -0,0 +1 @@ +<% render :collection => @commands %> \ No newline at end of file diff --git a/app/views/home/index.erb b/app/views/home/index.erb deleted file mode 100644 index 3b12464..0000000 --- a/app/views/home/index.erb +++ /dev/null @@ -1 +0,0 @@ -TEST \ No newline at end of file diff --git a/app/views/layouts/_menu.erb b/app/views/layouts/_menu.erb new file mode 100644 index 0000000..aa15921 --- /dev/null +++ b/app/views/layouts/_menu.erb @@ -0,0 +1 @@ +<%= link_to "CmdLiner fancy logo", root_path %> \ No newline at end of file diff --git a/app/views/layouts/application.erb b/app/views/layouts/application.erb index 5a7e785..4081d59 100644 --- a/app/views/layouts/application.erb +++ b/app/views/layouts/application.erb @@ -1,16 +1,20 @@ - CMDLiner + <%=title%> | CmdLiner.com <%= css_tag 'reset' %>
- <%= render 'layouts/user' %> +
+ <%= render 'layouts/menu' %> + <%= render 'layouts/user' %> +
<%= render 'layouts/flash' %>
+

<%=title%>

<%= yield %>
diff --git a/config/routes.rb b/config/routes.rb index 5f1c921..409b53a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ ActionController::Routing::Routes.draw do |map| - map.root :controller => 'home' + map.root :controller => :commands map.login '/login', :controller => "users", :action => "new" map.resources :users + map.resources :commands, :only=>[:index, :show] end \ No newline at end of file diff --git a/db/migrate/20090913103817_create_commands.rb b/db/migrate/20090913103817_create_commands.rb new file mode 100644 index 0000000..1e5493e --- /dev/null +++ b/db/migrate/20090913103817_create_commands.rb @@ -0,0 +1,17 @@ +class CreateCommands < ActiveRecord::Migration + def self.up + create_table :commands do |t| + t.string :name, :null=>false + t.text :description, :null=>false + t.boolean :mac_only, :default=>false, :null=>false + t.timestamps + end + + # enforce uniqueness + add_index :commands, :name, :unique => true + end + + def self.down + drop_table :commands + end +end diff --git a/db/schema.rb b/db/schema.rb index 4293583..8c4b760 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,17 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20090910191750) do +ActiveRecord::Schema.define(:version => 20090913103817) do + + create_table "commands", :force => true do |t| + t.string "name", :null => false + t.text "description", :null => false + t.boolean "mac_only", :default => false, :null => false + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "commands", ["name"], :name => "index_commands_on_name", :unique => true create_table "users", :force => true do |t| t.string "name", :null => false diff --git a/spec/factories.rb b/spec/factories.rb index e1bdfc1..c9f1ff5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -9,4 +9,9 @@ Factory.define(:user) do |f| f.identifier { Factory.next(:identifier) } f.name { Factory.next(:name) } +end + +Factory.define(:command) do |f| + f.name 'ls' + f.description 'show files n stuff' end \ No newline at end of file diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 0000000..84ed2d6 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,32 @@ +require 'spec/spec_helper' + +describe ApplicationHelper do + include ApplicationHelper + + describe :title do + before do + stub!(:current_model).and_return User + end + + it "fetches the title from @title" do + @title = 'YEP' + title.should == @title + end + + it "gets the title from current_object" do + user = Factory(:user) + @current_object = user + title.should == user.to_s + end + + it "gets the title from new objects" do + user = Factory.build(:user) + @current_object = user + title.should == "New User" + end + + it "gets the title from current_model" do + title.should == 'Users' + end + end +end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d8cd0ed..dd48e68 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -13,6 +13,16 @@ behaves_like_owner_changeable(user) end + it "Command" do + behaves_like_static(Factory(:command)) + end + + def behaves_like_static(item) + check_can(:read, true, true, true, item) + check_can(:write, false, false, false, item) + check_can(:create, false, false, false, item) + end + def behaves_like_owner_changeable(item) check_can(:read, true, true, true, item) check_can(:write, true, false, false, item)