Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about slug generation #1

Open
terenceponce opened this issue Mar 23, 2013 · 1 comment
Open

Question about slug generation #1

terenceponce opened this issue Mar 23, 2013 · 1 comment
Labels

Comments

@terenceponce
Copy link
Member

I'm trying to improve some of the model tests by using shoulda-matchers and I came across something interesting about slug generation.

I tried changing this part:

describe 'with blank title' do
  before { @article.title = ' ' }
  it { should_not be_valid }
end

into this:

it { should validate_presence_of(:title) }

and I got this error:

Failure/Error: it { should validate_presence_of(:title) }
NoMethodError:
undefined method to_slug for nil:NilClass
# ./app/models/article.rb:40:in generate_slug
# ./spec/models/article_spec.rb:39:in block (2 levels) in <top (required)>

The shoulda-matcher, validate_presence_of is passing nil to test presence validation, but it turns out, slug generation happens before validation and the error is happening because :title is nil. Does generate_slug really have to happen before validation or can we put it in another callback?

@bryanbibat
Copy link
Member

The only reason generate_slug is done before validation is so that a slug can be generated on create and update (e.g. the user deleted the slug on update for it to re-generate a new one).

Feel free to add nil checking or some other approach as long as this behavior (generating slug on create and update) is implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants