1module AdminHelper
 
2  ISO8601_TIMESTAMP = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\z/
 
3
  • Complexity 1 » saikuro
4  def selected_tags
 
5    @selected_tags ||= Array(params[:tags]).flatten.map(&:to_i).compact.reject(&:zero?)
 
6  end
 
7
  • Complexity 39 » saikuro
8  def mandatory_field
 
 9    content_tag :span, raw('*'), class: 'mandatory'
 
10  end
 
  • DuplicateMethodCall - calls 'options[:model_name]' 2 times » reek
  • DuplicateMethodCall - calls 'options[:url]' 2 times » reek
12  def cms_delete_link(model, options = {})
 
13    options[:model_name] ||= model.name
 
14    options[:url] ||= resource_path(model)
 
15    link_to image_tag('admin/delete.png', :size => "16x16", :alt => "Delete"), options[:url], :data => {
 
16                        :confirm => "WARNING: This action cannot be undone.\nAre you sure you want to delete #{h options[:model_name]}?",
 
17                        :method => :delete
 
18                      }
 
19  end
 
 
21  def admin_petition_facets_for_select(facets, selected)
 
22    options = admin_petition_facets.map do |facet|
 
23      [I18n.t(facet,  scope: :"petitions.facets.names.admin", quantity: facets[facet]), facet]
 
24    end
 
 
26    options_for_select(options, selected)
 
27  end
 
 
29  def admin_signature_states_for_select(selected)
 
30    options_for_select(I18n.t(:states, scope: :"admin.signature"), selected)
 
31  end
 
 
33  def admin_archived_petition_facets_for_select(facets, selected)
 
34    options = admin_archived_petition_facets.map do |facet|
 
35      [I18n.t(facet,  scope: :"petitions.facets.names.admin_archived", quantity: facets[facet]), facet]
 
36    end
 
 
38    options_for_select(options, selected)
 
39  end
 
 
41  def admin_invalidation_facets_for_select(facets, selected)
 
42    options = admin_invalidation_facets.map do |facet|
 
43      [I18n.t(facet,  scope: :"admin.invalidations.facets.labels", quantity: facets[facet]), facet]
 
44    end
 
 
46    options_for_select(options, selected)
 
47  end
 
 
49  def admin_parliaments_for_select(selected)
 
50    options_from_collection_for_select(archived_parliaments, :id, :name, selected)
 
51  end
 
  • DuplicateMethodCall - calls 'petition.signature_count' 2 times » reek
53  def email_petitioners_with_count_submit_button(form, petition, options = {})
 
54    i18n_options = {
 
55      scope: :admin, count: petition.signature_count,
 
56      formatted_count: number_with_delimiter(petition.signature_count)
 
57    }
 
 
59    html_options = {
 
60      name: 'save_and_email', class: 'button',
 
61      data: { confirm: t(:email_confirm, i18n_options) }
 
62    }.merge(options)
 
 
64    form.submit(t(:email_button, i18n_options), html_options)
 
65  end
 
 
67  def fraudulent_domains?(since: 1.hour.ago, limit: 10)
 
68    !fraudulent_domains(since: since, limit: limit).empty?
 
69  end
 
 
71  def fraudulent_domains(since: 1.hour.ago, limit: 10)
 
72    @fraudulent_domains ||= build_fraudulent_domains(since, limit)
 
73  end
 
 
75  def fraudulent_ips?(since: 1.hour.ago, limit: 10)
 
76    !fraudulent_ips(since: since, limit: limit).empty?
 
77  end
 
 
79  def fraudulent_ips(since: 1.hour.ago, limit: 10)
 
80    @fraudulent_ips ||= build_fraudulent_ips(since, limit)
 
81  end
 
 
83  def trending_domains(since: 1.hour.ago, limit: 10)
 
84    @trending_domains ||= build_trending_domains(since, limit)
 
85  end
 
 
87  def trending_domains?(since: 1.hour.ago, limit: 10)
 
88    !trending_domains(since: since, limit: limit).empty?
 
89  end
 
 
91  def trending_ips(since: 1.hour.ago, limit: 10)
 
92    @trending_ips ||= build_trending_ips(since, limit)
 
93  end
 
 
95  def trending_ips?(since: 1.hour.ago, limit: 10)
 
96    !trending_ips(since: since, limit: limit).empty?
 
97  end
 
  • DuplicateMethodCall - calls 'params[:window]' 2 times » reek
 99  def trending_window?
 
100    params[:window].present? && params[:window] =~ ISO8601_TIMESTAMP
 
101  end
 
 
103  def trending_window
 
104    if trending_window?
 
105      starts_at = params[:window].in_time_zone
 
106      ends_at = starts_at.advance(hours: 1)
 
 
108      starts_at..ends_at
 
109    end
 
110  end
 
 
112  def signature_count_interval_menu
 
113    {
 
114      "1 second" => "1",
 
115      "2 seconds" => "2",
 
116      "5 seconds" => "5",
 
117      "10 seconds" => "10",
 
118      "30 seconds" => "30",
 
119      "1 minute" => "60",
 
120      "5 minutes" => "300"
 
121    }
 
122  end
 
 
124  private
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
126  def admin_petition_facets
 
127    I18n.t(:admin, scope: :"petitions.facets")
 
128  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
130  def admin_archived_petition_facets
 
131    I18n.t(:admin_archived, scope: :"petitions.facets")
 
132  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
134  def admin_invalidation_facets
 
135    I18n.t(:keys, scope: :"admin.invalidations.facets")
 
136  end
 
 
138  def rate_limit
 
139    @rate_limit ||= RateLimit.first_or_create!
 
140  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
142  def build_fraudulent_domains(since, limit)
 
143    Signature.fraudulent_domains(since: since, limit: limit)
 
144  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
146  def build_fraudulent_ips(since, limit)
 
147    Signature.fraudulent_ips(since: since, limit: limit)
 
148  end
 
  • FeatureEnvy - refers to 'domains' more than self (maybe move it to another class?) » reek
  • NestedIterators - contains iterators nested 2 deep » reek
  • TooManyStatements - has approx 7 statements » reek
  • UncommunicativeVariableName - has the variable name 'd' » reek
150  def build_trending_domains(since, limit)
 
151    all_domains = Signature.trending_domains(since: since, limit: limit + 30)
 
152    allowed_domains = rate_limit.allowed_domains_list
 
 
154    all_domains.inject([]) do |domains, (domain, count)|
 
155      return domains if domains.size == limit
 
 
157      unless allowed_domains.any?{ |d| d === domain }
 
158        domains << [domain, count]
 
159      end
 
 
161      domains
 
162    end
 
163  end
 
  • FeatureEnvy - refers to 'ips' more than self (maybe move it to another class?) » reek
  • NestedIterators - contains iterators nested 2 deep » reek
  • TooManyStatements - has approx 7 statements » reek
  • UncommunicativeVariableName - has the variable name 'i' » reek
165  def build_trending_ips(since, limit)
 
166    all_ips = Signature.trending_ips(since: since, limit: limit + 30)
 
167    allowed_ips = rate_limit.allowed_ips_list
 
 
169    all_ips.inject([]) do |ips, (ip, count)|
 
170      return ips if ips.size == limit
 
 
172      unless allowed_ips.any?{ |i| i.include?(ip) }
 
173        ips << [ip, count]
 
174      end
 
 
176      ips
 
177    end
 
178  end
 
179end