Attribute not in model

24 views
Skip to first unread message

Diego Guebel

unread,
Sep 15, 2013, 4:50:29 AM9/15/13
to fabrica...@googlegroups.com
I'm using acts_as_taggable_on to tag some models. I've tried a few things but still straggling to fabricate a User model that is marked a acts_as_tagger and models that are marked as acts_as_taggable_on.

I'm using Rails 4, therefore attr_accessible is not longer allowed. The controller that creates this Model (i.e.: post) is expecting tag as a valid parameter and I do:

class PostController

def create
     ......
     @user.tag(@post, :with => @opts[:tag], :on => :category)
     ......
end

I'd like to fabricate Post passing different tags as parameter to being able to test i.e.: filter functions

I was expecting to do something like:

Fabricator(:post) do
   title
   author
   .....
   tag 'category 1, category 2'
end

But it fails because tag isn't in the model.

Thanks in advance.
D

Paul Elliott

unread,
Sep 15, 2013, 8:40:07 AM9/15/13
to fabrica...@googlegroups.com
You can only set attributes in the fabricator block, although you could leverage a callback to set the tags. I would declare a transient for the tag names and then apply then if present.

Fabricator(:post) do
  transient :tag_names
  author

  before_validation do |post, transients|
    transients[:tag_names].split(', ').each do |tag_name|
      post.author.tag(post, with: tag_name, on: :category)
    end if transients[:tag_names].present?
  end
end

I just wrote this off the top of my head so I apologize for anything invalid, but hopefully you get the idea. Let me know how it works out!

-- Paul

--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages