]> git.openstreetmap.org Git - rails.git/blob - app/helpers/user_blocks_helper.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / helpers / user_blocks_helper.rb
1 # frozen_string_literal: true
2
3 module UserBlocksHelper
4   include ActionView::Helpers::TranslationHelper
5
6   ##
7   # returns a translated string representing the status of the
8   # user block (i.e: whether it's active, what the expiry time is)
9   def block_status(block)
10     if block.active?
11       # if the block hasn't expired yet show the date, if the user just needs to login show that
12       if block.needs_view?
13         if block.ends_at > Time.now.utc
14           t("user_blocks.helper.time_future_and_until_login_html", :time => friendly_date(block.ends_at))
15         else
16           t("user_blocks.helper.until_login")
17         end
18       else
19         t("user_blocks.helper.time_future_html", :time => friendly_date(block.ends_at))
20       end
21     else
22       # the max of the last update time or the ends_at time is when this block finished
23       # either because the user viewed the block (updated_at) or it expired or was
24       # revoked (ends_at)
25       last_time = block.deactivates_at || [block.ends_at, block.updated_at].max
26       t("user_blocks.helper.time_past_html", :time => friendly_date_ago(last_time))
27     end
28   end
29
30   def block_short_status(block)
31     if block.active?
32       if block.ends_at > Time.now.utc
33         t("user_blocks.helper.short.active")
34       else
35         t("user_blocks.helper.short.active_until_read")
36       end
37     else
38       if block.revoker_id.nil?
39         if block.deactivates_at > block.ends_at
40           t("user_blocks.helper.short.read_html", :time => block_short_time_in_past(block.deactivates_at))
41         else
42           t("user_blocks.helper.short.ended")
43         end
44       else
45         t("user_blocks.helper.short.revoked_html", :name => link_to(block.revoker.display_name, block.revoker,
46                                                                     :class => "username d-inline-block text-truncate text-wrap align-bottom",
47                                                                     :dir => "auto"))
48       end
49     end
50   end
51
52   def block_short_time_in_future(time)
53     tag.time l(time.to_date),
54              :datetime => time.xmlschema,
55              :title => t("user_blocks.helper.short.time_in_future_title",
56                          :time_absolute => l(time, :format => :friendly),
57                          :time_relative => time_ago_in_words(time))
58   end
59
60   def block_short_time_in_past(time)
61     tag.time l(time.to_date),
62              :datetime => time.xmlschema,
63              :title => t("user_blocks.helper.short.time_in_past_title",
64                          :time_absolute => l(time, :format => :friendly),
65                          :time_relative => time_ago_in_words(time, :scope => :"datetime.distance_in_words_ago"))
66   end
67
68   def block_duration_in_words(duration)
69     # Ensure the requested duration isn't negative, even by a millisecond
70     duration = 0 if duration.negative?
71     parts = ActiveSupport::Duration.build(duration).parts
72     if duration < 1.day
73       t("user_blocks.helper.block_duration.hours", :count => parts.fetch(:hours, 0))
74     elsif duration < 1.week
75       t("user_blocks.helper.block_duration.days", :count => parts[:days])
76     elsif duration < 1.month
77       t("user_blocks.helper.block_duration.weeks", :count => parts[:weeks])
78     elsif duration < 1.year
79       t("user_blocks.helper.block_duration.months", :count => parts[:months])
80     else
81       t("user_blocks.helper.block_duration.years", :count => parts[:years])
82     end
83   end
84 end