social_stream-base 0.13.2 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,104 +31,102 @@ module SocialStream
31
31
  end
32
32
  end
33
33
 
34
- module InstanceMethods
35
- # Current subject represented by the user. Defaults to the own user
36
- def current_subject
37
- @current_subject ||=
38
- current_subject_from_params ||
39
- current_subject_from_session ||
40
- current_user
41
- end
42
-
43
- # Set represented subject
44
- def current_subject= instance
45
- session[:subject_type] = instance.class.to_s
46
- session[:subject_id] = instance.id
34
+ # Current subject represented by the user. Defaults to the own user
35
+ def current_subject
36
+ @current_subject ||=
37
+ current_subject_from_params ||
38
+ current_subject_from_session ||
39
+ current_user
40
+ end
47
41
 
48
- @current_subject = instance
49
- end
50
-
51
- def current_actor
52
- current_subject.actor
53
- end
42
+ # Set represented subject
43
+ def current_subject= instance
44
+ session[:subject_type] = instance.class.to_s
45
+ session[:subject_id] = instance.id
54
46
 
55
- # Returns the {SocialStream::Models::Subject subject} that is in the path, or
56
- # the {#current_subject} if some {User} is logged in.
57
- #
58
- # Requirements: the controller must inherit from +InheritedResources::Base+ and the method
59
- # {ClassMethods#belongs_to_subjects} must be called
60
- #
61
- # class PostsController < InheritedResources::Base
62
- # belongs_to_subjects :optional => true
63
- # end
64
- #
65
- # # /users/demo/posts
66
- # profile_subject #=> User demo
67
- #
68
- # # /groups/test/posts
69
- # profile_subject #=> Group test
70
- #
71
- # # /posts
72
- # profile_subject #=> current_subject
73
- #
74
- #
75
- def profile_subject
76
- @profile_subject ||= association_chain[-1] || current_subject
77
- end
47
+ @current_subject = instance
48
+ end
49
+
50
+ def current_actor
51
+ current_subject.actor
52
+ end
78
53
 
79
- # Go to sign in page if {#profile_subject} is blank
80
- def profile_subject!
81
- @profile_subject ||= association_chain[-1] || warden.authenticate!
82
- end
54
+ # Returns the {SocialStream::Models::Subject subject} that is in the path, or
55
+ # the {#current_subject} if some {User} is logged in.
56
+ #
57
+ # Requirements: the controller must inherit from +InheritedResources::Base+ and the method
58
+ # {ClassMethods#belongs_to_subjects} must be called
59
+ #
60
+ # class PostsController < InheritedResources::Base
61
+ # belongs_to_subjects :optional => true
62
+ # end
63
+ #
64
+ # # /users/demo/posts
65
+ # profile_subject #=> User demo
66
+ #
67
+ # # /groups/test/posts
68
+ # profile_subject #=> Group test
69
+ #
70
+ # # /posts
71
+ # profile_subject #=> current_subject
72
+ #
73
+ #
74
+ def profile_subject
75
+ @profile_subject ||= association_chain[-1] || current_subject
76
+ end
83
77
 
84
- # Profile subject is suitable for paths like:
85
- # /users/demo/posts
86
- #
87
- # This method tries {#profile_subject} first and then {#current_subject}
88
- def profile_or_current_subject
89
- profile_subject || current_subject
90
- rescue
91
- current_subject
92
- end
78
+ # Go to sign in page if {#profile_subject} is blank
79
+ def profile_subject!
80
+ @profile_subject ||= association_chain[-1] || warden.authenticate!
81
+ end
93
82
 
94
- # A {User} must be logged in and is equal to {#profile_subject}
95
- def profile_subject_is_current?
96
- user_signed_in? && profile_subject == current_subject
97
- end
83
+ # Profile subject is suitable for paths like:
84
+ # /users/demo/posts
85
+ #
86
+ # This method tries {#profile_subject} first and then {#current_subject}
87
+ def profile_or_current_subject
88
+ profile_subject || current_subject
89
+ rescue
90
+ current_subject
91
+ end
98
92
 
99
- # Override Cancan#current_ability method to use {#current_subject}
100
- def current_ability
101
- @current_ability ||=
102
- Ability.new(current_subject)
103
- end
93
+ # A {User} must be logged in and is equal to {#profile_subject}
94
+ def profile_subject_is_current?
95
+ user_signed_in? && profile_subject == current_subject
96
+ end
104
97
 
105
- private
98
+ # Override Cancan#current_ability method to use {#current_subject}
99
+ def current_ability
100
+ @current_ability ||=
101
+ Ability.new(current_subject)
102
+ end
106
103
 
107
- # Get represented subject from params[:s]
108
- def current_subject_from_params
109
- return unless params[:s].present?
104
+ private
110
105
 
111
- subject = Actor.find_by_slug!(params[:s]).subject
106
+ # Get represented subject from params[:s]
107
+ def current_subject_from_params
108
+ return unless params[:s].present?
112
109
 
113
- unless subject.represented_by?(current_user)
114
- raise CanCan::AccessDenied.new("Not authorized!", :represent, subject.name)
115
- end
110
+ subject = Actor.find_by_slug!(params[:s]).subject
116
111
 
117
- if subject != current_user
118
- flash[:notice] ||= ""
119
- flash[:notice] += t('representation.notice',
120
- :subject => subject.name)
121
- end
112
+ unless subject.represented_by?(current_user)
113
+ raise CanCan::AccessDenied.new("Not authorized!", :represent, subject.name)
114
+ end
122
115
 
123
- self.current_subject = subject
116
+ if subject != current_user
117
+ flash[:notice] ||= ""
118
+ flash[:notice] += t('representation.notice',
119
+ :subject => subject.name)
124
120
  end
125
121
 
126
- # Get represented subject from session
127
- def current_subject_from_session
128
- return unless session[:subject_type].present? && session[:subject_id].present?
122
+ self.current_subject = subject
123
+ end
129
124
 
130
- session[:subject_type].constantize.find session[:subject_id]
131
- end
125
+ # Get represented subject from session
126
+ def current_subject_from_session
127
+ return unless session[:subject_type].present? && session[:subject_id].present?
128
+
129
+ session[:subject_type].constantize.find session[:subject_id]
132
130
  end
133
131
  end
134
132
  end
@@ -8,21 +8,19 @@ module SocialStream
8
8
  before_filter :set_locale
9
9
  end
10
10
 
11
- module InstanceMethods
12
- # Set locale as per params, user preference or default
13
- def set_locale
14
- I18n.locale = params[:locale] || user_preferred_locale || extract_locale_from_accept_language_header || I18n.default_locale
15
- end
11
+ # Set locale as per params, user preference or default
12
+ def set_locale
13
+ I18n.locale = params[:locale] || user_preferred_locale || extract_locale_from_accept_language_header || I18n.default_locale
14
+ end
16
15
 
17
- private
18
- def extract_locale_from_accept_language_header
19
- return nil if request.env['HTTP_ACCEPT_LANGUAGE'].nil?
20
- (request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).map{|l| l.to_sym} & I18n.available_locales).first
21
- end
16
+ private
17
+ def extract_locale_from_accept_language_header
18
+ return nil if request.env['HTTP_ACCEPT_LANGUAGE'].nil?
19
+ (request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).map{|l| l.to_sym} & I18n.available_locales).first
20
+ end
22
21
 
23
- def user_preferred_locale
24
- current_user.language if user_signed_in?
25
- end
22
+ def user_preferred_locale
23
+ current_user.language if user_signed_in?
26
24
  end
27
25
  end
28
26
  end
@@ -25,14 +25,11 @@ module SocialStream
25
25
  end
26
26
  end
27
27
 
28
- module InstanceMethods
28
+ protected
29
29
 
30
- protected
31
-
32
- def set_author_ids
33
- resource_params.first[:author_id] = current_subject.try(:actor_id)
34
- resource_params.first[:user_author_id] = current_user.try(:actor_id)
35
- end
30
+ def set_author_ids
31
+ resource_params.first[:author_id] = current_subject.try(:actor_id)
32
+ resource_params.first[:user_author_id] = current_user.try(:actor_id)
36
33
  end
37
34
  end
38
35
  end
@@ -7,11 +7,9 @@ module SocialStream
7
7
  inherit_resources
8
8
  end
9
9
 
10
- module InstanceMethods
11
- # Overwrite {SocialStream::Controllers::Helpers::InstanceMethods#profile_subject}
12
- def profile_subject
13
- !resource.new_record? && resource || current_subject
14
- end
10
+ # Overwrite {SocialStream::Controllers::Helpers::InstanceMethods#profile_subject}
11
+ def profile_subject
12
+ !resource.new_record? && resource || current_subject
15
13
  end
16
14
  end
17
15
  end
@@ -3,6 +3,18 @@ module SocialStream
3
3
  # Models that have author, user_author and owner, properties saved in {Channel}.
4
4
  # Currently {Activity} and {ActivityObject}
5
5
  module Channeled
6
+ # Add the method {#channeled} to ActiveRecord
7
+ module ActiveRecord
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ # This class is channeled. See {Channel}
12
+ def channeled
13
+ include SocialStream::Models::Channeled
14
+ end
15
+ end
16
+ end
17
+
6
18
  extend ActiveSupport::Concern
7
19
 
8
20
  included do
@@ -14,36 +26,22 @@ module SocialStream
14
26
  before_validation :check_existing_channel
15
27
  end
16
28
 
17
- module InstanceMethods
18
-
19
- protected
29
+ protected
20
30
 
21
- # Use existing channel, do not create a new one
22
- def check_existing_channel
23
- return unless channel!.new_record?
31
+ # Use existing channel, do not create a new one
32
+ def check_existing_channel
33
+ return unless channel!.new_record?
24
34
 
25
- existing_channel =
26
- Channel.
27
- where(:author_id => author_id,
28
- :owner_id => owner_id,
29
- :user_author_id => user_author_id).
30
- first
35
+ existing_channel =
36
+ Channel.
37
+ where(:author_id => author_id,
38
+ :owner_id => owner_id,
39
+ :user_author_id => user_author_id).
40
+ first
31
41
 
32
- return if existing_channel.blank?
42
+ return if existing_channel.blank?
33
43
 
34
- self.channel = existing_channel
35
- end
36
- end
37
-
38
- module ActiveRecord
39
- extend ActiveSupport::Concern
40
-
41
- module ClassMethods
42
- # This class is channeled. See {Channel}
43
- def channeled
44
- include SocialStream::Models::Channeled
45
- end
46
- end
44
+ self.channel = existing_channel
47
45
  end
48
46
  end
49
47
  end
@@ -1,5 +1,3 @@
1
- require 'active_support/concern'
2
-
3
1
  module SocialStream
4
2
  module Models
5
3
  # Additional features for models that are Activity Objects
@@ -31,79 +29,77 @@ module SocialStream
31
29
  }
32
30
  end
33
31
 
34
- module InstanceMethods
35
- # Was the author represented with this {SocialStream::Models::Object object} was created?
36
- def represented_author?
37
- author_id == user_author_id
38
- end
32
+ # Was the author represented with this {SocialStream::Models::Object object} was created?
33
+ def represented_author?
34
+ author_id == user_author_id
35
+ end
39
36
 
40
- # All the activities with this object
41
- def activities
42
- Activity.
43
- includes(:activity_objects => self.class.to_s.underscore).
44
- where("#{ self.class.quoted_table_name }.id" => self.id)
45
- end
37
+ # All the activities with this object
38
+ def activities
39
+ Activity.
40
+ includes(:activity_objects => self.class.to_s.underscore).
41
+ where("#{ self.class.quoted_table_name }.id" => self.id)
42
+ end
46
43
 
47
- # Build the post activity when this object is not saved
48
- def build_post_activity
49
- Activity.new :author => author,
50
- :user_author => user_author,
51
- :owner => owner,
52
- :relation_ids => Array(_relation_ids)
53
- end
44
+ # Build the post activity when this object is not saved
45
+ def build_post_activity
46
+ Activity.new :author => author,
47
+ :user_author => user_author,
48
+ :owner => owner,
49
+ :relation_ids => Array(_relation_ids)
50
+ end
54
51
 
55
- def _contact
56
- @_contact ||= author && owner && author.contact_to!(owner)
57
- end
52
+ def _contact
53
+ @_contact ||= author && owner && author.contact_to!(owner)
54
+ end
58
55
 
59
- def _contact_id
60
- _contact.try(:id)
61
- end
56
+ def _contact_id
57
+ _contact.try(:id)
58
+ end
62
59
 
63
- def _relation_ids
64
- @_relation_ids ||=
65
- if _contact_id.nil?
66
- nil
60
+ def _relation_ids
61
+ @_relation_ids ||=
62
+ if _contact_id.nil?
63
+ nil
64
+ else
65
+ # FIXME: repeated in Activity#fill_relations
66
+ if _contact.reflexive?
67
+ _contact.sender.relation_customs.map(&:id)
67
68
  else
68
- # FIXME: repeated in Activity#fill_relations
69
- if _contact.reflexive?
70
- _contact.sender.relation_customs.map(&:id)
71
- else
72
- _contact.
73
- receiver.
74
- relation_customs.
75
- allow(_contact.sender, 'create', 'activity').
76
- map(&:id)
77
- end
69
+ _contact.
70
+ receiver.
71
+ relation_customs.
72
+ allow(_contact.sender, 'create', 'activity').
73
+ map(&:id)
78
74
  end
79
- end
75
+ end
76
+ end
80
77
 
81
- def _activity_parent
82
- @_activity_parent ||= Activity.find(_activity_parent_id)
83
- end
78
+ def _activity_parent
79
+ @_activity_parent ||= Activity.find(_activity_parent_id)
80
+ end
84
81
 
85
- private
82
+ private
86
83
 
87
- def create_post_activity
88
- create_activity "post"
89
- end
84
+ def create_post_activity
85
+ create_activity "post"
86
+ end
90
87
 
91
- def create_update_activity
92
- return if _contact_id.blank?
93
-
94
- create_activity "update"
95
- end
88
+ def create_update_activity
89
+ return if _contact_id.blank?
90
+
91
+ create_activity "update"
92
+ end
96
93
 
97
- def create_activity(verb)
98
- a = Activity.new :verb => verb,
99
- :channel => channel,
100
- :relation_ids => _relation_ids,
101
- :parent_id => _activity_parent_id
94
+ def create_activity(verb)
95
+ a = Activity.new :verb => verb,
96
+ :channel => channel,
97
+ :relation_ids => _relation_ids,
98
+ :parent_id => _activity_parent_id
102
99
 
103
- a.activity_objects << activity_object
100
+ a.activity_objects << activity_object
104
101
 
105
- a.save!
106
- end
102
+ a.save!
107
103
  end
108
104
  end
109
105
  end