Skip to content
Browse files

Created basic Company model

  • Loading branch information...
1 parent 8def1f2 commit 4b554ece66d3d0066a9b66daae52cf7c086820f3 @jcasimir committed
View
41 app/controllers/companies_controller.rb
@@ -0,0 +1,41 @@
+class CompaniesController < ApplicationController
+ def index
+ @companies = Company.all
+ end
+
+ def show
+ @company = Company.find(params[:id])
+ end
+
+ def new
+ @company = Company.new
+ end
+
+ def create
+ @company = Company.new(params[:company])
+ if @company.save
+ redirect_to @company, :notice => "Successfully created company."
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @company = Company.find(params[:id])
+ end
+
+ def update
+ @company = Company.find(params[:id])
+ if @company.update_attributes(params[:company])
+ redirect_to @company, :notice => "Successfully updated company."
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @company = Company.find(params[:id])
+ @company.destroy
+ redirect_to companies_url, :notice => "Successfully destroyed company."
+ end
+end
View
2 app/helpers/companies_helper.rb
@@ -0,0 +1,2 @@
+module CompaniesHelper
+end
View
3 app/models/company.rb
@@ -0,0 +1,3 @@
+class Company < ActiveRecord::Base
+ attr_accessible :name
+end
View
8 app/views/companies/_form.html.erb
@@ -0,0 +1,8 @@
+<%= form_for @company do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
View
8 app/views/companies/edit.html.erb
@@ -0,0 +1,8 @@
+<% title "Edit Company" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @company %> |
+ <%= link_to "View All", companies_path %>
+</p>
View
17 app/views/companies/index.html.erb
@@ -0,0 +1,17 @@
+<% title "Companies" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ </tr>
+ <% for company in @companies %>
+ <tr>
+ <td><%= company.name %></td>
+ <td><%= link_to "Show", company %></td>
+ <td><%= link_to "Edit", edit_company_path(company) %></td>
+ <td><%= link_to "Destroy", company, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Company", new_company_path %></p>
View
5 app/views/companies/new.html.erb
@@ -0,0 +1,5 @@
+<% title "New Company" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", companies_path %></p>
View
12 app/views/companies/show.html.erb
@@ -0,0 +1,12 @@
+<% title "Company" %>
+
+<p>
+ <strong>Name:</strong>
+ <%= @company.name %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_company_path(@company) %> |
+ <%= link_to "Destroy", @company, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", companies_path %>
+</p>
View
2 config/routes.rb
@@ -1,4 +1,6 @@
JSContact::Application.routes.draw do
+ resources :companies
+
resources :email_addresses
resources :phone_numbers
View
12 db/migrate/20110420135545_create_companies.rb
@@ -0,0 +1,12 @@
+class CreateCompanies < ActiveRecord::Migration
+ def self.up
+ create_table :companies do |t|
+ t.string :name
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :companies
+ end
+end
View
8 db/schema.rb
@@ -10,7 +10,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110419213031) do
+ActiveRecord::Schema.define(:version => 20110420135545) do
+
+ create_table "companies", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "email_addresses", :force => true do |t|
t.string "address"
View
57 spec/controllers/companies_controller_spec.rb
@@ -0,0 +1,57 @@
+# require File.dirname(__FILE__) + '/../spec_helper'
+#
+# describe CompaniesController do
+# fixtures :all
+# render_views
+#
+# it "index action should render index template" do
+# get :index
+# response.should render_template(:index)
+# end
+#
+# it "show action should render show template" do
+# get :show, :id => Company.first
+# response.should render_template(:show)
+# end
+#
+# it "new action should render new template" do
+# get :new
+# response.should render_template(:new)
+# end
+#
+# it "create action should render new template when model is invalid" do
+# Company.any_instance.stubs(:valid?).returns(false)
+# post :create
+# response.should render_template(:new)
+# end
+#
+# it "create action should redirect when model is valid" do
+# Company.any_instance.stubs(:valid?).returns(true)
+# post :create
+# response.should redirect_to(company_url(assigns[:company]))
+# end
+#
+# it "edit action should render edit template" do
+# get :edit, :id => Company.first
+# response.should render_template(:edit)
+# end
+#
+# it "update action should render edit template when model is invalid" do
+# Company.any_instance.stubs(:valid?).returns(false)
+# put :update, :id => Company.first
+# response.should render_template(:edit)
+# end
+#
+# it "update action should redirect when model is valid" do
+# Company.any_instance.stubs(:valid?).returns(true)
+# put :update, :id => Company.first
+# response.should redirect_to(company_url(assigns[:company]))
+# end
+#
+# it "destroy action should destroy model and redirect to index action" do
+# company = Company.first
+# delete :destroy, :id => company
+# response.should redirect_to(companies_url)
+# Company.exists?(company.id).should be_false
+# end
+# end
View
5 spec/fixtures/companies.yml
@@ -0,0 +1,5 @@
+one:
+ name: MyString
+
+two:
+ name: MyString
View
7 spec/models/company_spec.rb
@@ -0,0 +1,7 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Company do
+ it "should be valid" do
+ Company.new.should be_valid
+ end
+end

0 comments on commit 4b554ec

Please sign in to comment.
Something went wrong with that request. Please try again.