added tags schema and model

This commit is contained in:
Dan Elbert 2016-10-14 12:47:24 -05:00
parent e758af25b2
commit 45b0a9c7b1
6 changed files with 31 additions and 0 deletions

View File

View File

@ -4,6 +4,8 @@ class Recipe < ApplicationRecord
has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy has_many :recipe_steps, -> { order :sort_order }, inverse_of: :recipe, dependent: :destroy
belongs_to :user belongs_to :user
has_and_belongs_to_many :tags
scope :undeleted, -> { where('deleted <> ? OR deleted IS NULL', true) } scope :undeleted, -> { where('deleted <> ? OR deleted IS NULL', true) }
scope :not_log, -> { where('is_log <> ? OR is_log IS NULL', true) } scope :not_log, -> { where('is_log <> ? OR is_log IS NULL', true) }
scope :active, -> { undeleted.not_log } scope :active, -> { undeleted.not_log }

5
app/models/tag.rb Normal file
View File

@ -0,0 +1,5 @@
class Tag < ApplicationRecord
validates :name, presence: true, length: {maximum: 250}, uniqueness: { case_sensitive: false }
end

View File

@ -0,0 +1,14 @@
class CreateTags < ActiveRecord::Migration[5.0]
def change
create_table :tags do |t|
t.string :name, index: {unique: true}
t.timestamps
end
create_table :recipes_tags, id: false do |t|
t.integer :recipe_id, index: true
t.integer :tag_id, index: true
end
end
end

5
spec/factories/tags.rb Normal file
View File

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :tag do
name "MyString"
end
end

5
spec/models/tag_spec.rb Normal file
View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Tag, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end