Commit bc7c5f87 authored by Andrew8xx8's avatar Andrew8xx8

Project snippet moved to separate model

parent 7d2fbe6b
# == Schema Information
#
# Table name: snippets
#
# id :integer not null, primary key
# title :string(255)
# content :text
# author_id :integer not null
# project_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# file_name :string(255)
# expires_at :datetime
# type :string(255)
# private :boolean
class ProjectSnippet < Snippet
belongs_to :project
belongs_to :author, class_name: "User"
validates :project, presence: true
# Scopes
scope :fresh, -> { order("created_at DESC") }
scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
end
...@@ -11,21 +11,20 @@ ...@@ -11,21 +11,20 @@
# updated_at :datetime not null # updated_at :datetime not null
# file_name :string(255) # file_name :string(255)
# expires_at :datetime # expires_at :datetime
# # type :string(255)
# private :boolean
class Snippet < ActiveRecord::Base class Snippet < ActiveRecord::Base
include Linguist::BlobHelper include Linguist::BlobHelper
attr_accessible :title, :content, :file_name, :expires_at attr_accessible :title, :content, :file_name, :expires_at
belongs_to :project
belongs_to :author, class_name: "User" belongs_to :author, class_name: "User"
has_many :notes, as: :noteable, dependent: :destroy has_many :notes, as: :noteable, dependent: :destroy
delegate :name, :email, to: :author, prefix: true, allow_nil: true delegate :name, :email, to: :author, prefix: true, allow_nil: true
validates :author, presence: true validates :author, presence: true
validates :project, presence: true
validates :title, presence: true, length: { within: 0..255 } validates :title, presence: true, length: { within: 0..255 }
validates :file_name, presence: true, length: { within: 0..255 } validates :file_name, presence: true, length: { within: 0..255 }
validates :content, presence: true validates :content, presence: true
......
class AddTypeToSnippets < ActiveRecord::Migration
def change
add_column :snippets, :type, :string
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130323174317) do ActiveRecord::Schema.define(:version => 20130324151736) do
create_table "events", :force => true do |t| create_table "events", :force => true do |t|
t.string "target_type" t.string "target_type"
...@@ -191,6 +191,7 @@ ActiveRecord::Schema.define(:version => 20130323174317) do ...@@ -191,6 +191,7 @@ ActiveRecord::Schema.define(:version => 20130323174317) do
t.string "file_name" t.string "file_name"
t.datetime "expires_at" t.datetime "expires_at"
t.boolean "private" t.boolean "private"
t.string "type"
end end
add_index "snippets", ["created_at"], :name => "index_snippets_on_created_at" add_index "snippets", ["created_at"], :name => "index_snippets_on_created_at"
......
# == Schema Information
#
# Table name: snippets
#
# id :integer not null, primary key
# title :string(255)
# content :text
# author_id :integer not null
# project_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# file_name :string(255)
# expires_at :datetime
#
require 'spec_helper'
describe ProjectSnippet do
describe "Associations" do
it { should belong_to(:project) }
end
describe "Mass assignment" do
it { should_not allow_mass_assignment_of(:project_id) }
end
describe "Validation" do
it { should validate_presence_of(:project) }
end
end
...@@ -17,19 +17,16 @@ require 'spec_helper' ...@@ -17,19 +17,16 @@ require 'spec_helper'
describe Snippet do describe Snippet do
describe "Associations" do describe "Associations" do
it { should belong_to(:project) }
it { should belong_to(:author).class_name('User') } it { should belong_to(:author).class_name('User') }
it { should have_many(:notes).dependent(:destroy) } it { should have_many(:notes).dependent(:destroy) }
end end
describe "Mass assignment" do describe "Mass assignment" do
it { should_not allow_mass_assignment_of(:author_id) } it { should_not allow_mass_assignment_of(:author_id) }
it { should_not allow_mass_assignment_of(:project_id) }
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:author) } it { should validate_presence_of(:author) }
it { should validate_presence_of(:project) }
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_within(0..255) } it { should ensure_length_of(:title).is_within(0..255) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment