joshfrench / rakismet

Easy Akismet and TypePad AntiSpam integration for Rails
MIT License
355 stars 46 forks source link

Define raskimet attrs with accepts_nested_attributes_for #18

Closed localguiding closed 12 years ago

localguiding commented 12 years ago

Hi, first of all cool gem ! I have some doubts about how or where define akismet attributes when dealing with these 2 models:

class Conversation < ActiveRecord::Base
   has_many :messages
   accepts_nested_attributes_for :messages
end
class Message < ActiveRecord::Base
   belongs_to :conversation
   attr_accessor :body, ...
end

My idea is to call spam? before calling Conversation.save in my ConversationsController. The problem is that I don't know how match asktmet "content" attribute with Message.body

Should I do something like:

class Conversation < ActiveRecord::Base
   has_many :messages
   accepts_nested_attributes_for :messages
   include Rakismet::Model
   rakismet_attrs :content => => proc { message.body } # ??????????
end

Thanks in advance.

joshfrench commented 12 years ago

Hi Alvaro!

You probably want the Rakismet stuff on the Message class, since it seems like that's the individual object you want to test for spam. You could do it like this:

class Conversation < ActiveRecord::Base
   has_many :messages
   accepts_nested_attributes_for :messages
end

class Message < ActiveRecord::Base
    belongs_to :conversation
    include Rakismet::Model
    rakismet_attrs :content => :body
end

Then you could ask a conversation object if any of its messages are spam:

@conversation.messages.any? &:spam?

You could put that into a validation hook and do something (throw an error, mark it for review, etc.) if any messages are marked as spam.

Let me know if you have more questions!

localguiding commented 12 years ago

Thanks for you reply.

And yes, I have some other questions :) As you suggested I have included it in the model Message:

rakismet_attrs  :author => proc { conversation.contact_name },
                  :author_email => proc { conversation.contact_email },
                  :content => :body,
                  :comment_type => "ct"

As you can see, beside :content => :body, I have also matched rakismet attributes with the parent model (conversation) attributes. Is it ok to mix associated attributes and direct associations (i.e. :content => :body) as I did ?

Alvaro.

joshfrench commented 12 years ago

Yup, that's totally ok!

localguiding commented 12 years ago

Thanks you !