Class: Kanal::Plugins::UserSystem::Models::KanalUser
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Kanal::Plugins::UserSystem::Models::KanalUser
- Defined in:
- lib/kanal/plugins/user_system/models/kanal_user.rb
Overview
Base user class for storing user properties
Constant Summary collapse
- STATE_PROPERTY =
"_state"
Class Method Summary collapse
-
.find_all_by_property(property_name:, property_value: nil, limit: nil, offset: nil) ⇒ Array<Kanal::Plugins::UserSystem::Models::KanalUser>
Find all users that have the specified property.
-
.find_all_by_state(state_value, limit: nil, offset: nil) ⇒ Array<Kanal::Plugins::UserSystem::Models::KanalUser>
Find all users with needed state.
Instance Method Summary collapse
-
#create_or_update_property(property_name, property_value) ⇒ KanalUserProperty
Create KanalUserProperty and attach to this user Value of property can be any value that can be serialized and deserialized with JSON.
-
#get_property_by_name(property_name) ⇒ Kanal::Plugins::UserSystem::Models::KanalUserProperty?
Gets property by name if it exists for this user.
-
#properties ⇒ Array<Kanal::Plugins::UserSystem::Models::KanalUserProperty>
Get all relatived properties for this user.
-
#state ⇒ String
Get current users state.
-
#state=(state_value) ⇒ void
Set state for user.
Class Method Details
.find_all_by_property(property_name:, property_value: nil, limit: nil, offset: nil) ⇒ Array<Kanal::Plugins::UserSystem::Models::KanalUser>
Find all users that have the specified property. If you also specify the property_value, you will get users that also has the same value. You can also optionally use limit and offset for pagination or simply getting the preferred size of result
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 121 def self.find_all_by_property(property_name:, property_value: nil, limit: nil, offset: nil) if !property_value.nil? users = KanalUser.includes(:kanal_user_properties).where( "kanal_user_properties.name" => property_name.to_s, "kanal_user_properties.raw_value" => JSON.generate(property_value) ) else users = KanalUser.includes(:kanal_user_properties).where( "kanal_user_properties.name" => property_name.to_s ) end users.limit(limit) unless limit.nil? users.offset(offset) unless offset.nil? users end |
.find_all_by_state(state_value, limit: nil, offset: nil) ⇒ Array<Kanal::Plugins::UserSystem::Models::KanalUser>
Find all users with needed state. It’s possible to limit and offset
95 96 97 98 99 100 101 102 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 95 def self.find_all_by_state(state_value, limit: nil, offset: nil) find_all_by_property( property_name: STATE_PROPERTY, property_value: state_value, limit: limit, offset: offset ) end |
Instance Method Details
#create_or_update_property(property_name, property_value) ⇒ KanalUserProperty
Create KanalUserProperty and attach to this user Value of property can be any value that can be serialized and deserialized with JSON
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 48 def create_or_update_property(property_name, property_value) property = KanalUserProperty.find_by(kanal_user_id: id, name: property_name) property ||= KanalUserProperty.create( kanal_user_id: id, name: property_name, raw_value: JSON.generate(property_value) ) property.value = property_value property.save end |
#get_property_by_name(property_name) ⇒ Kanal::Plugins::UserSystem::Models::KanalUserProperty?
Gets property by name if it exists for this user
34 35 36 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 34 def get_property_by_name(property_name) KanalUserProperty.find_by(kanal_user_id: id, name: property_name) end |
#properties ⇒ Array<Kanal::Plugins::UserSystem::Models::KanalUserProperty>
Get all relatived properties for this user
23 24 25 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 23 def properties kanal_user_properties end |
#state ⇒ String
Get current users state
67 68 69 70 71 72 73 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 67 def state prop = get_property_by_name STATE_PROPERTY return nil if prop.nil? prop.value end |
#state=(state_value) ⇒ void
This method returns an undefined value.
Set state for user
82 83 84 |
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 82 def state=(state_value) create_or_update_property STATE_PROPERTY, state_value end |