Class: Candidate

Inherits:
ApplicationRecord show all
Includes:
Checkable, HasOwner, Toggleable
Defined in:
app/models/candidate.rb

Overview

Candidate for election campaign

Attributes:

about [text], optional
agent_id [Agent], optional
approved [boolean]
birthday [date]
campaign_id [Campaign]
created_at [DateTime]
data [JSON]
details_url [String]
image [SimpleImageUploader]
ip [Inet]
lead [text]
name [string]
patronymic [string], optional
program [text], optional
region_id [Region], optional
supports_impeachment [Boolean]
surname [string]
sync_state [JSON]
updated_at [DateTime]
user_id [User], optional
uuid [uuid]
visible [boolean]

Constant Summary collapse

ABOUT_LIMIT =
65_535
LEAD_LIMIT =
500
NAME_LIMIT =
50
URL_LIMIT =
255

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.creation_parameters(as_user = false) ⇒ Object



75
76
77
# File 'app/models/candidate.rb', line 75

def self.creation_parameters(as_user = false)
  entity_parameters(as_user) + %i[campaign_id]
end

.entity_parameters(as_user = false) ⇒ Object



68
69
70
71
72
73
# File 'app/models/candidate.rb', line 68

def self.entity_parameters(as_user = false)
  usual = %i[about birthday image lead name patronymic program region_id surname]
  extended = %i[approved supports_impeachment visible]

  as_user ? usual : usual + extended
end

Instance Method Details

#add_user(user, role = nil) ⇒ Object

Parameters:

  • user (User)
  • role (String) (defaults to: nil)


130
131
132
133
134
135
136
137
138
139
# File 'app/models/candidate.rb', line 130

def add_user(user, role = nil)
  return if user.nil?
  return unless CandidateUser::ROLES.include?(role)

  instance = candidate_users.find_or_initialize_by(user: user)
  instance.data['roles'] ||= {}
  instance.data['roles'][role] = true

  instance.save
end

#ageObject



91
92
93
94
95
96
97
98
# File 'app/models/candidate.rb', line 91

def age
  return 0 if birthday.blank?

  now = Time.now
  result = now.year - birthday.year
  result -= 1 if birthday.month > now.month || (birthday.month >= now.month && birthday.day > now.day)
  result
end

#assign_data_from_userObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/candidate.rb', line 100

def assign_data_from_user
  return if user.nil?

  profile = user.data['profile'] || {}
  new_data = {
    name: profile['name'],
    patronymic: profile['patronymic'],
    surname: profile['surname'],
    birthday: user.data['birthday']
  }

  assign_attributes(new_data)
end

#full_name(use_patronymic = false) ⇒ Object

Parameters:

  • use_patronymic (TrueClass|FalseClass) (defaults to: false)


80
81
82
83
84
85
# File 'app/models/candidate.rb', line 80

def full_name(use_patronymic = false)
  parts = [surname, name]
  parts << patronymic if use_patronymic

  parts.join(' ')
end

#image_alt_textObject



87
88
89
# File 'app/models/candidate.rb', line 87

def image_alt_text
  full_name(true)
end

#team_membership_available?(user, role) ⇒ Boolean

Parameters:

  • user (User)
  • role (String)

Returns:

  • (Boolean)


122
123
124
125
126
# File 'app/models/candidate.rb', line 122

def team_membership_available?(user, role)
  return false if user.nil?

  !user?(user, role)
end

#urlObject



141
142
143
# File 'app/models/candidate.rb', line 141

def url
  "/campaigns/#{campaign.slug}/candidate-#{id}"
end

#user?(user, role) ⇒ Boolean

Parameters:

  • user (User)
  • role (String)

Returns:

  • (Boolean)


116
117
118
# File 'app/models/candidate.rb', line 116

def user?(user, role)
  candidate_users.where(user: user).with_role(role).exists?
end