Class: Kanal::Plugins::UserSystem::Models::KanalUser

Inherits:
ActiveRecord::Base
  • Object
show all
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

Instance Method Summary collapse

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

Examples:

users_with_big_nose = KanalUser.find_all_by_property(property_name: "nose", property_value: "big")

Parameters:

  • property_name (String, Symbol)

    <description>

  • property_value (Object) (defaults to: nil)

    <description>

  • limit (Integer) (defaults to: nil)

    limit result

  • offset (Integer) (defaults to: nil)

    offset for result

Returns:



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

Parameters:

  • state_value (String)

    <description>

  • limit (Integer) (defaults to: nil)

    <description>

  • offset (Integer) (defaults to: nil)

    <description>

Returns:



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

Parameters:

  • property_name (String, Symbol)

    <description>

  • property_value (Object)

    <description>

Returns:



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

Parameters:

  • property_name (String, Symbol)

    <description>

Returns:



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

#propertiesArray<Kanal::Plugins::UserSystem::Models::KanalUserProperty>

Get all relatived properties for this user

Returns:



23
24
25
# File 'lib/kanal/plugins/user_system/models/kanal_user.rb', line 23

def properties
  kanal_user_properties
end

#stateString

Get current users state

Returns:

  • (String)

    <description>



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

Parameters:

  • value (String)

    <description>



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