ish_models 0.0.33.168 → 0.0.33.169

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f231fd1652da3cd2e8113a7963919aa0148becd53e53efc85d4cb8c2d45eddd0
4
- data.tar.gz: 77ef4b9331ef224679c2c3000e0738bba4e51be462bbddf854dc4c04c9338f80
3
+ metadata.gz: 1c0e3408ce26fe3d97602564c99955fa11a05c6584c2cf435a55edbbe2865c63
4
+ data.tar.gz: f984f36ddfc7d046a819d9966306d6e31192e008af037c77fad9bf2975557b42
5
5
  SHA512:
6
- metadata.gz: a26b8edb378762eb22caca40ebe5ee895dc16cb563332431b964769d94c87bb0873312c153a40cc162e779daf60db0085452ce46a6459c0c81f3a9dafe24dc77
7
- data.tar.gz: 2c6ebe91cefc06019d5090ded6f04bd95389002dc765cb359f3efdb46ab5092a72cbfd3202d6987283ae0a7aaa0590697976f0cae9d0abe1ecc87d3142970905
6
+ metadata.gz: 27f95caf60f39ce446b0b103be69cafc729a41dbd108833aefe5bcd3814ec1bba4499a5ad1943b4f7f4e5f728c798467c52fa60c43b3f37cf87675c61a149eae
7
+ data.tar.gz: 5a73b3adfe1953e2a3d84171880472dcfd6fe4bb0dfd615e000b0b7b53aec52eaba17383c6f6aaa73d36e4ba91df652e4d9b36524088eccaf070da893d0b75a5
@@ -0,0 +1,15 @@
1
+
2
+ ##
3
+ ## This shouldn't really be in Ish namespace... but oh well
4
+ ##
5
+
6
+ class Ish::EmailContext
7
+ include Mongoid::Document
8
+ include Mongoid::Timestamps
9
+
10
+ field :to_email
11
+ field :subject
12
+ field :template_name
13
+
14
+ end
15
+ EmailContext = Ish::EmailContext
@@ -0,0 +1,31 @@
1
+
2
+ class Ish::Meeting
3
+ include Mongoid::Document
4
+ include Mongoid::Timestamps
5
+
6
+ field :datetime, type: DateTime
7
+ field :invitee_email, type: String
8
+ field :invitee_name, type: String
9
+ field :timezone, type: String, default: 'Central Time (US & Canada)'
10
+ field :template_name
11
+
12
+ field :send_reminder_morning, type: Boolean, default: false
13
+ field :send_reminder_15min, type: Boolean, default: false
14
+
15
+ def self.template_name_options
16
+ [
17
+ [ '', '' ],
18
+ ['calendly_meeting_v1', 'calendly_meeting_v1'],
19
+ ['calendly_meeting_v2', 'calendly_meeting_v2'],
20
+ ]
21
+ end
22
+
23
+ def host_name
24
+ 'Victor Puudeyev'
25
+ end
26
+
27
+ def host_email
28
+ 'piousbox@gmail.com'
29
+ end
30
+
31
+ end
@@ -4,8 +4,16 @@ class Ish::Unsubscribe
4
4
  include Mongoid::Timestamps
5
5
 
6
6
  field :email
7
- field :unsubscribed_at
7
+ validates_presence_of :email
8
+ validates_uniqueness_of :email
9
+
10
+ field :mailer_type
11
+
8
12
  field :reason
9
13
 
14
+ field :unsubscribed_at
15
+
16
+
17
+
10
18
  end
11
19
 
@@ -1,10 +1,12 @@
1
1
 
2
2
  require 'ish/utils'
3
+ require 'mongoid/votable'
3
4
 
4
5
  ## @TODO: rename to Ish::Profile
5
6
  class Ish::UserProfile
6
7
  include Mongoid::Document
7
8
  include Mongoid::Timestamps
9
+ include Mongoid::Voter
8
10
  include Ish::Utils
9
11
 
10
12
  store_in collection: 'ish_user_profiles'
@@ -30,15 +32,17 @@ class Ish::UserProfile
30
32
 
31
33
  field :lang, default: 'en'
32
34
 
33
- ROLES = [ :admin, :manager, :guy ]
34
- ROLE_ADMIN = :admin
35
- ROLE_MANAGER = :manager
35
+ ROLES = [ :guy, :manager, :admin ]
36
36
  ROLE_GUY = :guy
37
- field :role_name, :type => Symbol
37
+ ROLE_MANAGER = :manager
38
+ ROLE_ADMIN = :admin
39
+ field :role_name, :type => Symbol, default: :guy
38
40
 
39
41
  has_one :profile_photo, :class_name => 'Photo', :inverse_of => :profile_city
40
42
 
41
43
  belongs_to :user
44
+ validates_presence_of :user
45
+
42
46
  belongs_to :current_city, :class_name => 'City', :inverse_of => :current_users, :optional => true
43
47
  belongs_to :guide_city, :class_name => 'City', :inverse_of => :guide, :optional => true
44
48
 
data/lib/ish_models.rb CHANGED
@@ -4,11 +4,13 @@ require 'ish/configuration'
4
4
 
5
5
  ::S3_CREDENTIALS ||= {}
6
6
 
7
- module CoTailors; end
8
7
  class Gameui; end
8
+
9
9
  module Ish; end
10
- # I need this thing for permissions#
10
+
11
+ # I need this thing for permissions???
11
12
  class Manager; end
13
+
12
14
  module Warbler; end
13
15
 
14
16
  module IshModels
@@ -26,9 +28,12 @@ module IshModels
26
28
  end
27
29
  end
28
30
 
29
- # Needs to be before gameui/asset3d
31
+ ## Needs to be before gameui/asset3d
30
32
  require 'ish/utils'
31
33
 
34
+ require 'mongoid/votable'
35
+ require 'mongoid/voter'
36
+
32
37
  require 'gameui/asset3d'
33
38
  require 'gameui/map'
34
39
  require 'gameui/map_bookmark'
@@ -38,12 +43,14 @@ require 'gameui/premium_purchase'
38
43
  require 'ish/cache_key'
39
44
  require 'ish/campaign'
40
45
  require 'ish/crawler'
46
+ require 'ish/email_context'
41
47
  require 'ish/gallery_name'
42
48
  require 'ish/image_asset'
43
49
  require 'ish/input_error'
44
50
  require 'ish/invoice'
45
51
  require 'ish/issue'
46
52
  require 'ish/lead'
53
+ require 'ish/meeting'
47
54
  require 'ish/payment'
48
55
  require 'ish/premium_item'
49
56
  require 'ish/unsubscribe'
@@ -65,3 +72,4 @@ require 'video'
65
72
 
66
73
 
67
74
 
75
+
@@ -0,0 +1,246 @@
1
+ module Mongoid
2
+ module Votable
3
+ extend ActiveSupport::Concern
4
+
5
+ # How many points should be assigned for each up or down vote.
6
+ # This array should be manipulated using Votable.vote_point method
7
+ VOTE_POINT = {}
8
+
9
+ included do
10
+ field :votable, type: Hash
11
+
12
+ index({ "votable.votes_count" => 1 })
13
+ index({ "votable.votes_point" => 1 })
14
+
15
+ scope :most_voted, -> { order_by(["votable.votes_count", :desc]) }
16
+ scope :best_voted, -> { order_by(["votable.votes_point", :desc]) }
17
+
18
+ # Set vote point for each up (down) vote on an object of this class
19
+ def self.vote_point(klass = self, options = nil)
20
+ VOTE_POINT[self.name] ||= {}
21
+ VOTE_POINT[self.name][klass.name] ||= options
22
+ end
23
+
24
+ #
25
+ # We usually need to show current_user his voting value on votable object
26
+ # voting value can be nil (not voted yet), :up or :down
27
+ # from voting value, we can decide it should be new vote or revote with :up or :down
28
+ # In this case, validation can be skip to maximize performance
29
+ #
30
+ # Make a vote on an object of this class
31
+ #
32
+ # @param [Hash] options a hash containings:
33
+ # - :votee_id: the votee document id
34
+ # - :voter_id: the voter document id
35
+ # - :value: :up or :down
36
+ # - :revote: change from vote up to vote down
37
+ # - :unvote: unvote the vote value (:up or :down)
38
+ #
39
+ def self.vote(options)
40
+ options.symbolize_keys!
41
+ value = options[:value]
42
+ votee_id = options[:votee_id]
43
+ voter_id = options[:voter_id]
44
+
45
+ klass = options[:class]
46
+ klass ||= VOTE_POINT.keys.include?(name) ? name : collection.name.classify
47
+ value_point = VOTE_POINT[klass][klass]
48
+
49
+ if options[:revote]
50
+ if value == :up
51
+ positive_field = :up_voter_ids
52
+ negative_field = :down_voter_ids
53
+ point_delta = value_point[:up] - value_point[:down]
54
+ else
55
+ positive_field = :down_voter_ids
56
+ negative_field = :up_voter_ids
57
+ point_delta = -value_point[:up] + value_point[:down]
58
+ end
59
+
60
+ update_result = collection.update_one({
61
+ # Validate voter_id did a vote with value for votee_id
62
+ :_id => votee_id,
63
+ "votable.#{positive_field.to_s}" => { '$ne' => voter_id },
64
+ "votable.#{negative_field.to_s}" => voter_id
65
+ }, {
66
+ # then update
67
+ '$pull' => { "votable.#{negative_field.to_s}" => voter_id },
68
+ '$push' => { "votable.#{positive_field.to_s}" => voter_id },
69
+ '$inc' => {
70
+ "votable.votes_point" => point_delta
71
+ }
72
+ }, {
73
+ :safe => true
74
+ })
75
+
76
+ elsif options[:unvote]
77
+ if value == :down
78
+ positive_field = :down_voter_ids
79
+ negative_field = :up_voter_ids
80
+ else
81
+ positive_field = :up_voter_ids
82
+ negative_field = :down_voter_ids
83
+ end
84
+
85
+ # Check if voter_id did a vote with value for votee_id
86
+ update_result = collection.update_one({
87
+ # Validate voter_id did a vote with value for votee_id
88
+ :_id => votee_id,
89
+ "votable.#{negative_field.to_s}" => { '$ne' => voter_id },
90
+ "votable.#{positive_field.to_s}" => voter_id
91
+ }, {
92
+ # then update
93
+ '$pull' => { "votable.#{positive_field.to_s}" => voter_id },
94
+ '$inc' => {
95
+ "votable.votes_count" => -1,
96
+ "votable.votes_point" => -value_point[value]
97
+ }
98
+ }, {
99
+ :safe => true
100
+ })
101
+
102
+ else # new vote
103
+ if value == :up
104
+ positive_field = :up_voter_ids
105
+ else
106
+ positive_field = :down_voter_ids
107
+ end
108
+
109
+ update_result = collection.update_one({
110
+ # Validate voter_id did not vote for votee_id yet
111
+ :_id => votee_id,
112
+ "votable.up_voter_ids" => { '$ne' => voter_id },
113
+ "votable.down_voter_ids" => { '$ne' => voter_id }
114
+ }, {
115
+ # then update
116
+ '$push' => { "votable.#{positive_field.to_s}" => voter_id },
117
+ '$inc' => {
118
+ "votable.votes_count" => +1,
119
+ "votable.votes_point" => value_point[value] }
120
+ }, {
121
+ :safe => true
122
+ })
123
+ end
124
+
125
+ # Only update parent class if votee is updated successfully
126
+ success = ( update_result.documents[0]['err'] == nil and
127
+ update_result.documents[0]['nModified'] == 1 and
128
+ update_result.documents[0]['n'] == 1 )
129
+
130
+ if success
131
+ VOTE_POINT[klass].each do |class_name, value_point|
132
+ # For other class in VOTE_POINT options, if is parent of current class
133
+ next unless relation_metadata = relations[class_name.underscore]
134
+ votee ||= options[:votee] || find(options[:votee_id])
135
+ # If can find current votee foreign_key value for that class
136
+ next unless foreign_key_value = votee.read_attribute(relation_metadata.foreign_key.to_sym)
137
+
138
+ # Update that class / collection
139
+ inc_options = if options[:revote]
140
+ {
141
+ "votable.votes_point" => ( value == :up ?
142
+ value_point[:up] - value_point[:down] :
143
+ -value_point[:up] + value_point[:down] )
144
+ }
145
+ elsif options[:unvote]
146
+ {
147
+ "votable.votes_count" => value_point[:not_increase_votes_count] ? 0 : -1,
148
+ "votable.votes_point" => -value_point[value]
149
+ }
150
+ else
151
+ {
152
+ "votable.votes_count" => value_point[:not_increase_votes_count] ? 0 : 1,
153
+ "votable.votes_point" => value_point[value]
154
+ }
155
+ end
156
+
157
+ class_name.constantize.collection.update_one(
158
+ { :_id => foreign_key_value },
159
+ { '$inc' => inc_options }
160
+ )
161
+ end
162
+ end
163
+
164
+ success
165
+ end
166
+
167
+ end
168
+
169
+ # Make a vote on this votee
170
+ #
171
+ # @param [Hash] options a hash containings:
172
+ # - :voter_id: the voter document id
173
+ # - :value: vote :up or vote :down
174
+ def vote(options)
175
+ options[:votee_id] ||= _id
176
+ options[:votee] ||= self
177
+
178
+ if options[:unvote]
179
+ options[:value] ||= vote_value(options[:voter_id])
180
+ else
181
+ options[:revote] ||= !vote_value(options[:voter_id]).nil?
182
+ end
183
+
184
+ self.class.vote(options)
185
+ end
186
+
187
+ # Get a voted value on this votee
188
+ #
189
+ # Let's NOT CONVERT bson::objectid's to strings. _vp_ 2022-08-31
190
+ #
191
+ # @param [Mongoid Object, BSON::ObjectId] voter is Mongoid object the id of the voter who made the vote
192
+ def vote_value(voter)
193
+ voter_id = voter.is_a?(BSON::ObjectId) ? voter : voter.is_a?(String) ? voter : voter.id
194
+
195
+ return :up if up_voter_ids.try(:include?, voter_id)
196
+ return :down if down_voter_ids.try(:include?, voter_id)
197
+ end
198
+
199
+ # Get the number of up votes
200
+ def up_votes_count
201
+ up_voter_ids.length
202
+ end
203
+
204
+ # Get the number of down votes
205
+ def down_votes_count
206
+ down_voter_ids.length
207
+ end
208
+
209
+ # Get the number of votes count
210
+ def votes_count
211
+ if self.has_attribute?(:votable)
212
+ self["votable"]["votes_count"]
213
+ else
214
+ 0
215
+ end
216
+ end
217
+
218
+ # Get the votes point
219
+ def votes_point
220
+ if self.has_attribute?(:votable)
221
+ self["votable"]["votes_point"]
222
+ else
223
+ 0
224
+ end
225
+ end
226
+ alias :votes_score :votes_point
227
+
228
+ # Array of up voter ids
229
+ def up_voter_ids
230
+ if self.has_attribute?(:votable)
231
+ self['votable']["up_voter_ids"]
232
+ else
233
+ []
234
+ end
235
+ end
236
+
237
+ # Array of down voter ids
238
+ def down_voter_ids
239
+ if self.has_attribute?(:votable)
240
+ self["votable"]["down_voter_ids"]
241
+ else
242
+ []
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,96 @@
1
+ module Mongoid
2
+ module Voter
3
+ extend ActiveSupport::Concern
4
+
5
+ # Get list of voted votees
6
+ #
7
+ # @param [Class] klass the votee class, e.g. `Post` or `Comment`
8
+ # @return [Array, nil] an array of votable objects voted by this voter
9
+ def votees(klass)
10
+ # Is this id, or id.to_s ?
11
+ # let's not convert to string, lets use bson::objectid.
12
+ # _vp_ 2022-08-31
13
+ klass.any_of({ "votable.up_voter_ids" => _id }, { "votable.down_voter_ids" => _id })
14
+ end
15
+
16
+ # Check to see if this voter voted on the votee or not
17
+ #
18
+ # @param [Hash, Object] options the hash containing the votee, or the votee itself
19
+ # @return [true, false] true if voted, false otherwise
20
+ def voted?(options)
21
+ unless options.is_a?(Hash)
22
+ votee_class = options.class
23
+ votee_id = options._id
24
+ else
25
+ votee = options[:votee]
26
+ if votee
27
+ votee_class = votee.class
28
+ votee_id = votee._id
29
+ else
30
+ votee_class = options[:votee_type].classify.constantize
31
+ votee_id = options[:votee_id]
32
+ end
33
+ end
34
+
35
+ votees(votee_class).where(:_id => votee_id).count == 1
36
+ end
37
+
38
+ # Get the voted value on a votee
39
+ #
40
+ # @param (see #voted?)
41
+ # @return [Symbol, nil] :up or :down or nil if not voted
42
+ def vote_value(options)
43
+ votee = unless options.is_a?(Hash)
44
+ options
45
+ else
46
+ options[:votee] || options[:votee_type].classify.constantize.only(:up_vote_ids, :down_vote_ids).where(
47
+ :_id => options[:votee_id]
48
+ ).first
49
+ end
50
+ votee.vote_value(_id)
51
+ end
52
+
53
+ # Cancel the vote on a votee
54
+ #
55
+ # @param [Object] votee the votee to be unvoted
56
+ def unvote(options)
57
+ unless options.is_a?(Hash)
58
+ options = { :votee => options }
59
+ end
60
+ options[:unvote] = true
61
+ options[:revote] = false
62
+ vote(options)
63
+ end
64
+
65
+ # Vote on a votee
66
+ #
67
+ # @param (see #voted?)
68
+ # @param [:up, :down] vote_value vote up or vote down, nil to unvote
69
+ def vote(options, value = nil)
70
+ if options.is_a?(Hash)
71
+ votee = options[:votee]
72
+ else
73
+ votee = options
74
+ options = { :votee => votee, :value => value }
75
+ end
76
+
77
+ if votee
78
+ options[:votee_id] = votee._id
79
+ votee_class = votee.class
80
+ else
81
+ votee_class = options[:votee_type].classify.constantize
82
+ end
83
+
84
+ if options[:value].nil?
85
+ options[:unvote] = true
86
+ options[:value] = vote_value(options)
87
+ else
88
+ options[:revote] = options.has_key?(:revote) ? !options[:revote].blank? : voted?(options)
89
+ end
90
+
91
+ options[:voter_id] = _id
92
+
93
+ ( votee || votee_class ).vote(options)
94
+ end
95
+ end
96
+ end
data/lib/newsitem.rb CHANGED
@@ -1,11 +1,15 @@
1
1
 
2
2
  require 'ish/utils'
3
+ # require_relative './mongoid/votable.rb'
3
4
 
4
5
  class Newsitem
5
6
  include Mongoid::Document
6
7
  include Mongoid::Timestamps
7
8
  include Ish::Utils
8
9
 
10
+ include Mongoid::Votable
11
+ vote_point self, :up => +1, :down => -1
12
+
9
13
  belongs_to :city, optional: true
10
14
  belongs_to :gallery, optional: true # seems correct. _vp_ 2022-03-21
11
15
  def gallery
data/lib/video.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'mongoid/paranoia'
2
+ require_relative './mongoid/votable.rb'
2
3
 
3
4
  class Video
4
5
  include Mongoid::Document
@@ -7,6 +8,9 @@ class Video
7
8
  include Mongoid::Paranoia
8
9
  include Ish::Utils
9
10
 
11
+ include Mongoid::Votable
12
+ vote_point self, :up => +1, :down => -1
13
+
10
14
  PER_PAGE = 6
11
15
 
12
16
  field :name, :type => String
@@ -101,4 +105,6 @@ class Video
101
105
  %w| name descr |
102
106
  end
103
107
 
108
+
109
+
104
110
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ish_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.33.168
4
+ version: 0.0.33.169
5
5
  platform: ruby
6
6
  authors:
7
7
  - piousbox
@@ -131,12 +131,14 @@ files:
131
131
  - lib/ish/campaign.rb
132
132
  - lib/ish/configuration.rb
133
133
  - lib/ish/crawler.rb
134
+ - lib/ish/email_context.rb
134
135
  - lib/ish/gallery_name.rb
135
136
  - lib/ish/image_asset.rb
136
137
  - lib/ish/input_error.rb
137
138
  - lib/ish/invoice.rb
138
139
  - lib/ish/issue.rb
139
140
  - lib/ish/lead.rb
141
+ - lib/ish/meeting.rb
140
142
  - lib/ish/payment.rb
141
143
  - lib/ish/premium_item.rb
142
144
  - lib/ish/railtie.rb
@@ -144,6 +146,8 @@ files:
144
146
  - lib/ish/user_profile.rb
145
147
  - lib/ish/utils.rb
146
148
  - lib/ish_models.rb
149
+ - lib/mongoid/votable.rb
150
+ - lib/mongoid/voter.rb
147
151
  - lib/newsitem.rb
148
152
  - lib/photo.rb
149
153
  - lib/report.rb