Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished 10.1.3

  • Loading branch information...
commit ff1a81dd67b0315d651ea755cd1def2424235a42 1 parent 08d2523
@mashbash authored
View
6 app/models/micropost.rb
@@ -0,0 +1,6 @@
+class Micropost < ActiveRecord::Base
+ attr_accessible :content
+ belongs_to :user
+
+ validates :user_id, presence: true
+end
View
1  app/models/user.rb
@@ -12,6 +12,7 @@
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
+ has_many :microposts
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
View
11 db/migrate/20120610212005_create_microposts.rb
@@ -0,0 +1,11 @@
+class CreateMicroposts < ActiveRecord::Migration
+ def change
+ create_table :microposts do |t|
+ t.string :content
+ t.integer :user_id
+
+ t.timestamps
+ end
+ add_index :microposts, [:user_id, :created_at]
+ end
+end
View
11 db/schema.rb
@@ -11,7 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120609221514) do
+ActiveRecord::Schema.define(:version => 20120610212005) do
+
+ create_table "microposts", :force => true do |t|
+ t.string "content"
+ t.integer "user_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "microposts", ["user_id", "created_at"], :name => "index_microposts_on_user_id_and_created_at"
create_table "users", :force => true do |t|
t.string "name"
View
31 spec/models/micropost_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Micropost do
+
+ let(:user) { FactoryGirl.create(:user) }
+ before { @micropost = user.microposts.build(content: "Lorem ipsum") }
+
+
+ subject { @micropost }
+
+ it { should respond_to(:content) }
+ it { should respond_to(:user_id) }
+ it { should respond_to(:user) }
+ its(:user) { should == user }
+
+ it { should be_valid }
+
+ describe "accessible attributes" do
+ it "should not allow access to user_id" do
+ expect do
+ Micropost.new(user_id: user.id)
+ end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
+ end
+ end
+
+ describe "when user_id is not present" do
+ before { @micropost.user_id = nil }
+ it { should_not be_valid }
+ end
+
+end
View
7 spec/models/user_spec.rb
@@ -20,18 +20,17 @@
subject { @user }
-#added from listing 8.15
- it { should respond_to(:password_confirmation) }
- it { should respond_to(:remember_token) }
- it { should respond_to(:authenticate) }
+#added from listing 8.15
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
+ it { should respond_to(:remember_token) }
it { should respond_to(:admin) }
it { should respond_to(:authenticate) }
+ it { should respond_to(:microposts) }
it { should be_valid }
# calls valid ? subject on test
Please sign in to comment.
Something went wrong with that request. Please try again.