Commit bc7c5f87bbd5cc25a0aaf03e9e5ecf6a65375098
1 parent
7d2fbe6b
Exists in
master
and in
4 other branches
Project snippet moved to separate model
Showing
6 changed files
with
66 additions
and
7 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | +# == Schema Information | |
| 2 | +# | |
| 3 | +# Table name: snippets | |
| 4 | +# | |
| 5 | +# id :integer not null, primary key | |
| 6 | +# title :string(255) | |
| 7 | +# content :text | |
| 8 | +# author_id :integer not null | |
| 9 | +# project_id :integer not null | |
| 10 | +# created_at :datetime not null | |
| 11 | +# updated_at :datetime not null | |
| 12 | +# file_name :string(255) | |
| 13 | +# expires_at :datetime | |
| 14 | +# type :string(255) | |
| 15 | +# private :boolean | |
| 16 | + | |
| 17 | +class ProjectSnippet < Snippet | |
| 18 | + belongs_to :project | |
| 19 | + belongs_to :author, class_name: "User" | |
| 20 | + | |
| 21 | + validates :project, presence: true | |
| 22 | + | |
| 23 | + # Scopes | |
| 24 | + scope :fresh, -> { order("created_at DESC") } | |
| 25 | + scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) } | |
| 26 | + scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) } | |
| 27 | +end | ... | ... |
app/models/snippet.rb
| ... | ... | @@ -11,21 +11,20 @@ |
| 11 | 11 | # updated_at :datetime not null |
| 12 | 12 | # file_name :string(255) |
| 13 | 13 | # expires_at :datetime |
| 14 | -# | |
| 14 | +# type :string(255) | |
| 15 | +# private :boolean | |
| 15 | 16 | |
| 16 | 17 | class Snippet < ActiveRecord::Base |
| 17 | 18 | include Linguist::BlobHelper |
| 18 | 19 | |
| 19 | 20 | attr_accessible :title, :content, :file_name, :expires_at |
| 20 | 21 | |
| 21 | - belongs_to :project | |
| 22 | 22 | belongs_to :author, class_name: "User" |
| 23 | 23 | has_many :notes, as: :noteable, dependent: :destroy |
| 24 | 24 | |
| 25 | 25 | delegate :name, :email, to: :author, prefix: true, allow_nil: true |
| 26 | 26 | |
| 27 | 27 | validates :author, presence: true |
| 28 | - validates :project, presence: true | |
| 29 | 28 | validates :title, presence: true, length: { within: 0..255 } |
| 30 | 29 | validates :file_name, presence: true, length: { within: 0..255 } |
| 31 | 30 | validates :content, presence: true | ... | ... |
db/schema.rb
| ... | ... | @@ -11,7 +11,7 @@ |
| 11 | 11 | # |
| 12 | 12 | # It's strongly recommended to check this file into your version control system. |
| 13 | 13 | |
| 14 | -ActiveRecord::Schema.define(:version => 20130323174317) do | |
| 14 | +ActiveRecord::Schema.define(:version => 20130324151736) do | |
| 15 | 15 | |
| 16 | 16 | create_table "events", :force => true do |t| |
| 17 | 17 | t.string "target_type" |
| ... | ... | @@ -191,6 +191,7 @@ ActiveRecord::Schema.define(:version => 20130323174317) do |
| 191 | 191 | t.string "file_name" |
| 192 | 192 | t.datetime "expires_at" |
| 193 | 193 | t.boolean "private" |
| 194 | + t.string "type" | |
| 194 | 195 | end |
| 195 | 196 | |
| 196 | 197 | add_index "snippets", ["created_at"], :name => "index_snippets_on_created_at" | ... | ... |
| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | +# == Schema Information | |
| 2 | +# | |
| 3 | +# Table name: snippets | |
| 4 | +# | |
| 5 | +# id :integer not null, primary key | |
| 6 | +# title :string(255) | |
| 7 | +# content :text | |
| 8 | +# author_id :integer not null | |
| 9 | +# project_id :integer not null | |
| 10 | +# created_at :datetime not null | |
| 11 | +# updated_at :datetime not null | |
| 12 | +# file_name :string(255) | |
| 13 | +# expires_at :datetime | |
| 14 | +# | |
| 15 | + | |
| 16 | +require 'spec_helper' | |
| 17 | + | |
| 18 | +describe ProjectSnippet do | |
| 19 | + describe "Associations" do | |
| 20 | + it { should belong_to(:project) } | |
| 21 | + end | |
| 22 | + | |
| 23 | + describe "Mass assignment" do | |
| 24 | + it { should_not allow_mass_assignment_of(:project_id) } | |
| 25 | + end | |
| 26 | + | |
| 27 | + describe "Validation" do | |
| 28 | + it { should validate_presence_of(:project) } | |
| 29 | + end | |
| 30 | +end | ... | ... |
spec/models/snippet_spec.rb
| ... | ... | @@ -17,19 +17,16 @@ require 'spec_helper' |
| 17 | 17 | |
| 18 | 18 | describe Snippet do |
| 19 | 19 | describe "Associations" do |
| 20 | - it { should belong_to(:project) } | |
| 21 | 20 | it { should belong_to(:author).class_name('User') } |
| 22 | 21 | it { should have_many(:notes).dependent(:destroy) } |
| 23 | 22 | end |
| 24 | 23 | |
| 25 | 24 | describe "Mass assignment" do |
| 26 | 25 | it { should_not allow_mass_assignment_of(:author_id) } |
| 27 | - it { should_not allow_mass_assignment_of(:project_id) } | |
| 28 | 26 | end |
| 29 | 27 | |
| 30 | 28 | describe "Validation" do |
| 31 | 29 | it { should validate_presence_of(:author) } |
| 32 | - it { should validate_presence_of(:project) } | |
| 33 | 30 | |
| 34 | 31 | it { should validate_presence_of(:title) } |
| 35 | 32 | it { should ensure_length_of(:title).is_within(0..255) } | ... | ... |