Module: SocialStream::Controllers::Helpers::InstanceMethods

Defined in:
lib/social_stream/controllers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#current_abilityObject

Override Cancan#current_ability method to use #current_subject



100
101
102
103
# File 'lib/social_stream/controllers/helpers.rb', line 100

def current_ability
  @current_ability ||=
    Ability.new(current_subject)
end

#current_actorObject



51
52
53
# File 'lib/social_stream/controllers/helpers.rb', line 51

def current_actor
  current_subject.actor
end

#current_subjectObject

Current subject represented by the user. Defaults to the own user



36
37
38
39
40
41
# File 'lib/social_stream/controllers/helpers.rb', line 36

def current_subject
  @current_subject ||=
    current_subject_from_params  ||
    current_subject_from_session ||
      current_user
end

#current_subject=(instance) ⇒ Object

Set represented subject



44
45
46
47
48
49
# File 'lib/social_stream/controllers/helpers.rb', line 44

def current_subject= instance
  session[:subject_type] = instance.class.to_s
  session[:subject_id]   = instance.id

  @current_subject = instance
end

#profile_or_current_subjectObject

Profile subject is suitable for paths like:

/users/demo/posts

This method tries #profile_subject first and then #current_subject



88
89
90
91
92
# File 'lib/social_stream/controllers/helpers.rb', line 88

def profile_or_current_subject
  profile_subject || current_subject
rescue
  current_subject
end

#profile_subjectObject

Returns the subject that is in the path, or the #current_subject if some User is logged in.

Requirements: the controller must inherit from InheritedResources::Base and the method ClassMethods#belongs_to_subjects must be called

class PostsController < InheritedResources::Base
  belongs_to_subjects :optional => true
end

# /users/demo/posts
profile_subject #=> User demo

# /groups/test/posts
profile_subject #=> Group test

# /posts
profile_subject #=> current_subject


75
76
77
# File 'lib/social_stream/controllers/helpers.rb', line 75

def profile_subject
  @profile_subject ||= association_chain[-1] || current_subject
end

#profile_subject!Object

Go to sign in page if #profile_subject is blank



80
81
82
# File 'lib/social_stream/controllers/helpers.rb', line 80

def profile_subject!
  @profile_subject ||= association_chain[-1] || warden.authenticate!
end

#profile_subject_is_current?Boolean

A User must be logged in and is equal to #profile_subject

Returns:

  • (Boolean)


95
96
97
# File 'lib/social_stream/controllers/helpers.rb', line 95

def profile_subject_is_current?
  user_signed_in? && profile_subject == current_subject
end