Module: Rostra::ActsAsRostra::InstanceMethods

Defined in:
lib/rostra.rb

Instance Method Summary collapse

Instance Method Details

#can_participate_in_rostra?Boolean

By default anyone can read rostra content - even if they are not logged in. This method determines if users can contribute content (i.e. ask questions, give answers, and leave comments). If can_participate_in_rostra? returns true, the user can contribute content, otherwise they’re restricted to read only.

Under the hood, Rostra uses CanCan to set permissions and this is the only place where can_participate_in_rostra? is actully used. The default is very simple, if the user exists (i.e. they are logged in), then they may participate. But you can override this method in your User model to set more complicated conditions participation. If you need to specify even more complicated permissions, override app/models/rostra/ability.rb and go wild.

Returns:

  • (Boolean)


85
86
87
# File 'lib/rostra.rb', line 85

def can_participate_in_rostra?
  !new_record?
end

#following?(question) ⇒ Boolean

Check if the user is following a given question

Returns:

  • (Boolean)


69
70
71
# File 'lib/rostra.rb', line 69

def following?(question)
  followed_questions.include?(question)
end

#rostra_user_emailObject

Rostra will use this method to find a user’s email address.



97
98
99
# File 'lib/rostra.rb', line 97

def rostra_user_email
  send(Rostra::Config.rostra_user_email)
end

#rostra_user_nameObject

Rostra will use this method to find a user’s full name.



91
92
93
# File 'lib/rostra.rb', line 91

def rostra_user_name
  send(Rostra::Config.rostra_user_name)
end

#vote_on(resource, direction) ⇒ Object

Built atop the thumbs_up API, this method does some conditional voting:

* Clear the user's existing vote if...
  - they've already voted up and attempt to vote up again
  - they've already voted down and attempt to vote down again
* Ensure the user only votes once per resource (i.e. question or answer)
* Clear previous votes before casting a new vote (e.g. they've already voted up and
  click to vote down, then clear the old up vote prior to casting the new down vote)


57
58
59
60
61
62
63
64
65
# File 'lib/rostra.rb', line 57

def vote_on(resource, direction)
  direction = direction.to_sym
  if (direction == :up && voted_for?(resource)) || (direction == :down && voted_against?(resource))
    clear_votes(resource)
  else
    vote(resource, {:exclusive => true, :direction => direction})
  end
  resource.reload
end