2013-03-01 06:07:44 -06:00
|
|
|
require_dependency 'enum'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-09-04 17:42:18 -05:00
|
|
|
class InvalidTrustLevel < StandardError; end
|
2014-11-17 08:02:10 -06:00
|
|
|
|
2013-03-01 06:07:44 -06:00
|
|
|
class TrustLevel
|
2014-11-17 08:02:10 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
attr_reader :id, :name
|
|
|
|
|
2013-02-28 07:08:56 -06:00
|
|
|
class << self
|
2014-09-04 17:42:18 -05:00
|
|
|
|
2014-11-17 08:02:10 -06:00
|
|
|
def [](level)
|
|
|
|
raise InvalidTrustLevel if !valid?(level)
|
2014-09-05 00:20:39 -05:00
|
|
|
level
|
2014-09-04 17:42:18 -05:00
|
|
|
end
|
|
|
|
|
2013-02-28 07:08:56 -06:00
|
|
|
def levels
|
2015-11-23 02:31:14 -06:00
|
|
|
@levels ||= Enum.new(:newuser, :basic, :member, :regular, :leader, start: 0)
|
2013-02-28 07:08:56 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
|
|
|
levels.map do |name_key, id|
|
|
|
|
TrustLevel.new(name_key, id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-05 00:20:39 -05:00
|
|
|
def valid?(level)
|
2014-12-09 11:16:47 -06:00
|
|
|
valid_range === level
|
2014-09-05 00:20:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_range
|
2014-11-17 08:02:10 -06:00
|
|
|
(0..4)
|
2013-02-28 07:08:56 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def compare(current_level, level)
|
2014-09-05 00:20:39 -05:00
|
|
|
(current_level || 0) >= level
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-11-17 08:02:10 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(name_key, id)
|
2013-02-25 10:42:20 -06:00
|
|
|
@name = I18n.t("trust_levels.#{name_key}.title")
|
2013-02-05 13:16:51 -06:00
|
|
|
@id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
def serializable_hash
|
2013-02-28 07:08:56 -06:00
|
|
|
{ id: @id, name: @name }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-11-17 08:02:10 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|