diff --git a/app/models/project_snippet.rb b/app/models/project_snippet.rb new file mode 100644 index 00000000000..a86f2e7a32f --- /dev/null +++ b/app/models/project_snippet.rb @@ -0,0 +1,27 @@ +# == 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 diff --git a/app/models/snippet.rb b/app/models/snippet.rb index c4ee35e0556..592dfdfbb57 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -11,21 +11,20 @@ # updated_at :datetime not null # file_name :string(255) # expires_at :datetime -# +# type :string(255) +# private :boolean class Snippet < ActiveRecord::Base include Linguist::BlobHelper attr_accessible :title, :content, :file_name, :expires_at - belongs_to :project belongs_to :author, class_name: "User" has_many :notes, as: :noteable, dependent: :destroy delegate :name, :email, to: :author, prefix: true, allow_nil: true validates :author, presence: true - validates :project, presence: true validates :title, presence: true, length: { within: 0..255 } validates :file_name, presence: true, length: { within: 0..255 } validates :content, presence: true diff --git a/db/migrate/20130324151736_add_type_to_snippets.rb b/db/migrate/20130324151736_add_type_to_snippets.rb new file mode 100644 index 00000000000..276aab2ca15 --- /dev/null +++ b/db/migrate/20130324151736_add_type_to_snippets.rb @@ -0,0 +1,5 @@ +class AddTypeToSnippets < ActiveRecord::Migration + def change + add_column :snippets, :type, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index a48b85c153e..88e249047e6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| t.string "target_type" @@ -191,6 +191,7 @@ ActiveRecord::Schema.define(:version => 20130323174317) do t.string "file_name" t.datetime "expires_at" t.boolean "private" + t.string "type" end add_index "snippets", ["created_at"], :name => "index_snippets_on_created_at" diff --git a/spec/models/project_snippet_spec.rb b/spec/models/project_snippet_spec.rb new file mode 100644 index 00000000000..716fd81c91b --- /dev/null +++ b/spec/models/project_snippet_spec.rb @@ -0,0 +1,30 @@ +# == 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 diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index e4d1934829f..52355c38f0c 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -17,19 +17,16 @@ require 'spec_helper' describe Snippet do describe "Associations" do - it { should belong_to(:project) } it { should belong_to(:author).class_name('User') } it { should have_many(:notes).dependent(:destroy) } end describe "Mass assignment" do it { should_not allow_mass_assignment_of(:author_id) } - it { should_not allow_mass_assignment_of(:project_id) } end describe "Validation" do it { should validate_presence_of(:author) } - it { should validate_presence_of(:project) } it { should validate_presence_of(:title) } it { should ensure_length_of(:title).is_within(0..255) }