Module: SpreeCmCommissioner::UserDecorator
- Defined in:
- app/models/spree_cm_commissioner/user_decorator.rb
Class Method Summary collapse
-
.prepended(base) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
Instance Method Summary collapse
- #consumed_timestep ⇒ Object
- #display_name ⇒ Object
- #early_adopter? ⇒ Boolean
- #ensure_unique_database_delivery_method(attributes) ⇒ Object
- #full_name ⇒ Object
-
#has_spree_role?(role_name) ⇒ Boolean
override.
- #invalidate_qr_data ⇒ Object
- #normal_user? ⇒ Boolean
- #normalize_email ⇒ Object
- #operator? ⇒ Boolean
- #organizer? ⇒ Boolean
- #otp_required_for_login ⇒ Object
- #otp_secret ⇒ Object
- #permissions_for_vendor(vendor_id) ⇒ Object
- #privileged_role? ⇒ Boolean
- #push_notificable? ⇒ Boolean
- #qr_data ⇒ Object
- #requires_strong_password? ⇒ Boolean
-
#set_login ⇒ Object
override to auto-generate clean usernames (eg. JohnTraveler1 format).
- #soft_deleted? ⇒ Boolean
- #super_admin? ⇒ Boolean
- #update_otp_enabled ⇒ Object
- #update_tracked_fields!(request) ⇒ Object
- #validate_current_password!(password) ⇒ Object
- #vendor_organizer?(vendor) ⇒ Boolean
Class Method Details
.prepended(base) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 5 def self.prepended(base) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength base.include SpreeCmCommissioner::UserNotification base.include SpreeCmCommissioner::UserIdentity base.include SpreeCmCommissioner::UserPreference base.include SpreeCmCommissioner::StoreMetadata base.include SpreeCmCommissioner::ServiceType base.enum gender: %i[male female other] base.enum registered_by: { self_registered: 0, system_registered: 1 }, _prefix: true base.has_many :subscriptions, through: :customer, class_name: 'SpreeCmCommissioner::Subscription' base.has_many :payments, as: :payable, class_name: 'Spree::Payment', dependent: :nullify base.has_many :role_permissions, through: :spree_roles, class_name: 'SpreeCmCommissioner::RolePermission' base.has_many :permissions, through: :role_permissions, class_name: 'SpreeCmCommissioner::Permission' base.has_many :line_items, through: :orders, source: :line_items base.has_many :check_ins, foreign_key: 'check_in_by_id', class_name: 'SpreeCmCommissioner::CheckIn' base.has_many :user_events, class_name: 'SpreeCmCommissioner::UserEvent' base.has_many :events, through: :user_events, class_name: 'Spree::Taxon', source: 'taxon' base.has_many :guests, class_name: 'SpreeCmCommissioner::Guest', dependent: :destroy base.has_many :saved_guests, class_name: 'SpreeCmCommissioner::SavedGuest', dependent: :nullify base.has_many :google_user_identity_providers, -> { where(identity_type: :google) }, class_name: 'SpreeCmCommissioner::UserIdentityProvider' base.has_many :wished_items, class_name: 'Spree::WishedItem', through: :wishlists base.has_many :promotions, through: :promotion_rules, class_name: 'Spree::Promotion' base.has_many :user_agencies, class_name: 'SpreeCmCommissioner::UserAgency' base.has_many :user_places, class_name: 'SpreeCmCommissioner::UserPlace' base.has_many :places, through: :user_places, class_name: 'SpreeCmCommissioner::Place' base.has_many :voting_credits, class_name: 'SpreeCmCommissioner::VotingCredit', dependent: :destroy base.has_many :votes, class_name: 'SpreeCmCommissioner::Vote', dependent: :destroy base.has_many :voting_credit_transactions, through: :voting_credits, class_name: 'SpreeCmCommissioner::VotingCreditTransaction' base.has_one :profile, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::UserProfile' base.has_one :customer, class_name: 'SpreeCmCommissioner::Customer' base.has_secure_password :confirm_pin_code, validations: false base.multi_tenant :tenant, class_name: 'SpreeCmCommissioner::Tenant' base.scope :by_tenant, -> (tenant_id) { where(tenant_id: tenant_id) } base.scope :by_non_tenant, -> { where(tenant_id: nil) } base.scope :vendor_users, -> (vendor_id) { joins(:role_users => :role).where(spree_roles: { vendor_id: vendor_id }).distinct } base.store :public_metadata, accessors: [:has_incomplete_guest_info], coder: JSON base.store :private_metadata, accessors: %i[otp_secret otp_required_for_login consumed_timestep], coder: JSON base. :primary_service_type, :string base. :qr_data_version, :integer, default: 1 base. :qr_data_invalidated_at, :datetime base. :branch_ids, :array, default: [] # Validations base.validates :primary_service_type, inclusion: { in: SpreeCmCommissioner::ServiceType::SERVICE_TYPES.map(&:to_s) }, allow_nil: true base.validates_password_strength :password, if: -> { requires_strong_password? && password.present? } base.before_validation :normalize_email, prepend: true # Login/username affect QR data, so invalidate it on change. base.before_validation :invalidate_qr_data, if: :will_save_change_to_login? base.before_save :update_otp_enabled base.attr_accessor :assigned_roles base.whitelisted_ransackable_attributes = %w[email first_name last_name gender phone_number] base.devise :two_factor_authenticatable def base.end_users joins('LEFT JOIN spree_vendor_users ON spree_users.id = spree_vendor_users.user_id').where(spree_vendor_users: { user_id: nil }) end def normal_user? system_user = admin? || !vendors.empty? normal_user = spree_roles.length == 1 ? spree_roles[0].name == 'user' : spree_roles.empty? normal_user && !system_user end def soft_deleted? !account_deletion_at.nil? end end |
Instance Method Details
#consumed_timestep ⇒ Object
107 108 109 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 107 def consumed_timestep ['consumed_timestep'] end |
#display_name ⇒ Object
160 161 162 163 164 165 166 167 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 160 def display_name return full_name if full_name.present? return first_name if first_name.present? return last_name if last_name.present? return email if email.present? phone_number end |
#early_adopter? ⇒ Boolean
129 130 131 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 129 def early_adopter? has_spree_role?('early_adopter') end |
#ensure_unique_database_delivery_method(attributes) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 174 def ensure_unique_database_delivery_method(attributes) recipient = self = { recipient_id: recipient.id, notificable_id: attributes[:notificable].id, notificable_type: attributes[:notificable].class.to_s, type: attributes[:type] } notification = recipient.notifications.where().first_or_initialize if notification.persisted? notification.update(attributes) else notification.assign_attributes(attributes) notification.save! end notification end |
#full_name ⇒ Object
147 148 149 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 147 def full_name [first_name, last_name].compact_blank.join(' ') end |
#has_spree_role?(role_name) ⇒ Boolean
override
121 122 123 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 121 def has_spree_role?(role_name) # rubocop:disable Naming/PredicateName spree_roles.non_vendor.exists?(name: role_name) end |
#invalidate_qr_data ⇒ Object
169 170 171 172 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 169 def invalidate_qr_data self.qr_data_version += 1 self.qr_data_invalidated_at = Time.zone.now end |
#normal_user? ⇒ Boolean
80 81 82 83 84 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 80 def normal_user? system_user = admin? || !vendors.empty? normal_user = spree_roles.length == 1 ? spree_roles[0].name == 'user' : spree_roles.empty? normal_user && !system_user end |
#normalize_email ⇒ Object
91 92 93 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 91 def normalize_email self.email = email&.strip&.downcase end |
#operator? ⇒ Boolean
137 138 139 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 137 def operator? has_spree_role?('operator') end |
#organizer? ⇒ Boolean
133 134 135 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 133 def organizer? has_spree_role?('organizer') end |
#otp_required_for_login ⇒ Object
103 104 105 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 103 def otp_required_for_login ['otp_required_for_login'] end |
#otp_secret ⇒ Object
99 100 101 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 99 def otp_secret ['otp_secret'] end |
#permissions_for_vendor(vendor_id) ⇒ Object
111 112 113 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 111 def (vendor_id) .joins(role_permissions: :role).where(spree_roles: { vendor_id: vendor_id }) end |
#privileged_role? ⇒ Boolean
151 152 153 154 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 151 def privileged_role? assigned = assigned_roles || [] assigned.map(&:to_s).include?('admin') end |
#push_notificable? ⇒ Boolean
196 197 198 199 200 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 196 def push_notificable? return false if device_tokens_count.blank? device_tokens_count.positive? end |
#qr_data ⇒ Object
95 96 97 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 95 def qr_data SpreeCmCommissioner::Users::QrData::Generate.call(user: self).value end |
#requires_strong_password? ⇒ Boolean
156 157 158 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 156 def requires_strong_password? admin? || privileged_role? end |
#set_login ⇒ Object
override to auto-generate clean usernames (eg. JohnTraveler1 format)
116 117 118 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 116 def set_login self.login ||= SpreeCmCommissioner::Users::Username::Generate.call(user: self) end |
#soft_deleted? ⇒ Boolean
86 87 88 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 86 def soft_deleted? !account_deletion_at.nil? end |
#super_admin? ⇒ Boolean
125 126 127 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 125 def super_admin? has_spree_role?('super_admin') end |
#update_otp_enabled ⇒ Object
208 209 210 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 208 def update_otp_enabled self.otp_enabled = otp_email || otp_phone_number end |
#update_tracked_fields!(request) ⇒ Object
212 213 214 215 216 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 212 def update_tracked_fields!(request) ActiveRecord::Base.connected_to(role: :writing) do super(request) end end |
#validate_current_password!(password) ⇒ Object
202 203 204 205 206 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 202 def validate_current_password!(password) return if valid_password?(password) errors.add(:password, I18n.t('spree_user.invalid_password')) end |
#vendor_organizer?(vendor) ⇒ Boolean
141 142 143 144 145 |
# File 'app/models/spree_cm_commissioner/user_decorator.rb', line 141 def vendor_organizer?(vendor) spree_roles.any? do |role| role.name == "#{vendor.id}-organizer" end end |