0

I have a model with custom slug with friendly id:

# == Schema Information
#
# Table name: blogs
#
#  id               :integer          not null, primary key
#  title            :string           not null
#  content          :text             not null
#  is_published     :boolean          default("false"), not null
#  slug             :string
#  publishing_date  :datetime
#  tags             :text             default("{}"), not null, is an Array
#  meta_keywords    :text             default("{}"), not null, is an Array
#  meta_description :text
#  page_title       :string(70)
#  author_id        :integer          default("1")
#  blog_category_id :integer          default("1")
#  author_type      :integer          default("0"), not null
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#

class Blog < ActiveRecord::Base
  enum author_type: [:content_contributor]
  enum publishing_status: [:reviewing, :accepted, :rejected]
  include FriendlyId
  friendly_id :title_and_category, use: [:slugged, :history]
  has_attached_file :featured_image,  styles: { large: '600x600>', medium: '300x300>',  thumb: '100x100>' }, default_url: 'No_image.jpg.png'
  validates_attachment_content_type :featured_image,  content_type: /\Aimage\/.*\Z/
  belongs_to :author, class_name: 'ContentContributor', foreign_key: 'author_id', inverse_of: :blogs
  belongs_to :featured_product, class_name: 'Product', foreign_key: 'featured_product_id', inverse_of: :blogs
  belongs_to :blog_category, inverse_of: :blogs
  belongs_to :deleted_by, class_name: 'User', foreign_key: 'deleted_by_id'
  validates :author, presence: true
  validates :author_type, presence: true
  validates :title, presence: true
  validates :content, presence: true
  validates :tags, presence: true
  validates :meta_keywords, presence: true

  def title_and_category
    "#{blog_category.name} #{title}"
  end
end

I have written spec for the model like this:

it { should define_enum_for(:author_type) }
  it { should define_enum_for(:publishing_status) }
  it { should belong_to(:author).class_name("ContentContributor").with_foreign_key("author_id").inverse_of(:blogs) }
  it { should belong_to(:featured_product).class_name("Product").with_foreign_key("featured_product_id").inverse_of(:blogs) }
  it { should belong_to(:deleted_by).class_name("User").with_foreign_key("deleted_by_id") }
  it { should belong_to(:blog_category).inverse_of(:blogs) }
  it { should validate_presence_of(:author) }
  it { should validate_presence_of(:author_type) }
  it { should validate_presence_of(:title) }
  it { should validate_presence_of(:content) }
  it { should validate_presence_of(:tags) }
  it { should validate_presence_of(:meta_keywords) }

The above fails with the following errors:

Failures:

  1) Blog should require author to be set
     Failure/Error: it { should validate_presence_of(:author) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./app/models/blog.rb:41:in `title_and_category'
     # ./spec/models/blog_spec.rb:33:in `block (2 levels) in <top (required)>'

  2) Blog should require author_type to be set
     Failure/Error: it { should validate_presence_of(:author_type) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./app/models/blog.rb:41:in `title_and_category'
     # ./spec/models/blog_spec.rb:34:in `block (2 levels) in <top (required)>'

  3) Blog should require title to be set
     Failure/Error: it { should validate_presence_of(:title) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./app/models/blog.rb:41:in `title_and_category'
     # ./spec/models/blog_spec.rb:35:in `block (2 levels) in <top (required)>'

  4) Blog should require content to be set
     Failure/Error: it { should validate_presence_of(:content) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./app/models/blog.rb:41:in `title_and_category'
     # ./spec/models/blog_spec.rb:36:in `block (2 levels) in <top (required)>'

  5) Blog should require tags to be set
     Failure/Error: it { should validate_presence_of(:tags) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./app/models/blog.rb:41:in `title_and_category'
     # ./spec/models/blog_spec.rb:37:in `block (2 levels) in <top (required)>'

  6) Blog should require meta_keywords to be set
     Failure/Error: it { should validate_presence_of(:meta_keywords) }
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./app/models/blog.rb:41:in `title_and_category'
     # ./spec/models/blog_spec.rb:38:in `block (2 levels) in <top (required)>'

I removed the code for custom name building and all the above passed but not with custom slug built. What should be changed?

Aravind
  • 1,391
  • 1
  • 16
  • 41

1 Answers1

4

It happens because FriendlyId uses a before_validation callback to set a slug. This method is invoked before any before_validation callbacks that you define in your class.

The title_and_category method is called before your other validations.

To fix this you can either invoke friendly_id after you set your before_validation callbacks, or you can just prepend your method before friendly_id:

Solution 1

Assuming you have a title_and_category column instead of a method.

# Make sure to prepend it so that it runs before Friendly_id's own callback
# http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

before_validation :set_title_and_category, prepend: true

def set_title_and_category
   self.title_and_category = "#{blog_category.name} #{title}"
end

Solution 2

def title_and_category
    "#{blog_category.name} #{title}" if blog_category
end

Solution 3

I see you are using shoulda-matchers, you could use shoulda callback matchers and add this code in your test

it { should callback(:generate_slug).before(:validation) } 
Vysakh Sreenivasan
  • 1,719
  • 1
  • 16
  • 27