Module: Clean::Support

Defined in:
lib/clean/support.rb

Class Method Summary collapse

Class Method Details

.actions_hash(options = {}) ⇒ Object

Lists all actions of all controller by loading them and list action_methods


192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/clean/support.rb', line 192

def actions_hash(options = {})
  controllers = controllers_in_file
  if options[:except]
    options[:except] = [options[:except]] unless options[:except].is_a?(Array)
    controllers.reject! { |c| options[:except].any? { |e| c <= e } }
  end
  ref = HashWithIndifferentAccess.new
  controllers.each do |controller|
    ref[controller.controller_path] = controller.action_methods.to_a.sort.delete_if { |a| a.to_s.start_with?('_') }
  end
  ref
end

.controllers_in_fileObject

Lists all controller that inherits of ApplicationController included


217
218
219
220
221
222
223
# File 'lib/clean/support.rb', line 217

def controllers_in_file
  Dir.glob(Rails.root.join('app', 'controllers', '**', '*.rb')).each { |file| require file }
  ObjectSpace
    .each_object(Class)
    .select { |klass| klass <= ::ApplicationController || klass <= ::ApiController }
    .sort { |a, b| a.name <=> b.name }
end

.deep_symbolize_keys(hash) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/clean/support.rb', line 49

def deep_symbolize_keys(hash)
  hash.inject({}) do |result, (key, value)|
    value = deep_symbolize_keys(value) if value.is_a? Hash
    key = :no if key.to_s == '__no_is_not__false__'
    result[(begin
              key.to_sym
            rescue
              key
            end) || key] = value
    result
  end
end

.default_action_title(controller_path, action_name) ⇒ Object


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/clean/support.rb', line 174

def default_action_title(controller_path, action_name)
  action_name = action_name.to_sym unless action_name.is_a?(Symbol)
  controller_name = controller_path.split('/').last
  text = if action_name == :index
           controller_name.humanize
         elsif action_name == :show
           controller_name.humanize.singularize + ': %{name}'
         elsif [:new].include? action_name
           "#{action_name} #{controller_name.humanize.singularize}".humanize
         elsif [:list, :import, :export].include? action_name
           "#{action_name} #{controller_name}".humanize
         else
           "#{action_name} #{controller_name.humanize.singularize}: %{name}".humanize
         end
  text
end

.hash_count(hash) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/clean/support.rb', line 41

def hash_count(hash)
  count = 0
  for key, value in hash
    count += (value.is_a?(Hash) ? hash_count(value) : 1)
  end
  count
end

.hash_diff(hash, ref, depth = 0, mode = nil) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/clean/support.rb', line 77

def hash_diff(hash, ref, depth = 0, mode = nil)
  hash ||= {}
  ref ||= {}
  keys = (ref.keys + hash.keys).uniq.sort { |a, b| a.to_s.tr('_', ' ').strip <=> b.to_s.tr('_', ' ').strip }
  code = ''
  count = 0
  total = 0
  for key in keys
    h = hash[key]
    r = ref[key]
    # total += 1 unless r.is_a? Hash
    if r.is_a?(Hash) && (h.is_a?(Hash) || h.nil?)
      scode, scount, stotal = hash_diff(h, r, depth + 1, mode)
      code << '  ' * depth + key.to_s + ":\n" + scode
      count += scount
      total += stotal
    elsif r && h.nil?
      code << '  ' * depth + '# ' + key.to_s + ': ' + (mode == :humanize ? key.to_s.humanize : yaml_value(r, depth + 1)) + "\n"
      count += 1
      total += 1
    elsif r && h && r.class == h.class
      code << '  ' * depth + key.to_s + ': ' + yaml_value(h, depth + 1) + "\n"
      total += 1
    elsif r && h && r.class != h.class
      code << '  ' * depth + key.to_s + ': ' + (yaml_value(h, depth) + "\n").gsub(/\n/, " #? #{r.class.name} excepted (#{h.class.name + ':' + h.inspect})\n")
      total += 1
    elsif h && r.nil?
      code << '  ' * depth + key.to_s + ': ' + (yaml_value(h, depth) + "\n").to_s.gsub(/\n/, " #?\n")
    elsif r.nil?
      code << '  ' * depth + key.to_s + ": #?\n"
    end
  end
  [code, count, total]
end

.hash_sort_and_count(hash, depth = 0) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clean/support.rb', line 24

def hash_sort_and_count(hash, depth = 0)
  hash ||= {}
  code = ''
  count = 0
  for key, value in hash.sort { |a, b| a[0].to_s <=> b[0].to_s }
    if value.is_a? Hash
      scode, scount = hash_sort_and_count(value, depth + 1)
      code += '  ' * depth + key.to_s + ":\n" + scode
      count += scount
    else
      code += '  ' * depth + key.to_s + ': ' + yaml_value(value, depth + 1) + "\n"
      count += 1
    end
  end
  [code, count]
end

.hash_to_yaml(hash, depth = 0) ⇒ Object


8
9
10
11
12
13
14
15
16
17
# File 'lib/clean/support.rb', line 8

def hash_to_yaml(hash, depth = 0)
  code = "\n"
  x = hash.to_a.sort { |a, b| a[0].to_s.tr('_', ' ').strip <=> b[0].to_s.tr('_', ' ').strip }
  x.each_index do |i|
    k = x[i][0]
    v = x[i][1]
    code += '  ' * depth + k.to_s + ':' + (v.is_a?(Hash) ? hash_to_yaml(v, depth + 1) : ' ' + yaml_value(v)) + (i == x.size - 1 ? '' : "\n") if v
  end
  code
end

.helpers_in_fileObject

Lists helpers paths


226
227
228
229
230
231
232
# File 'lib/clean/support.rb', line 226

def helpers_in_file
  dir = Rails.root.join('app', 'helpers')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.jobs_in_fileObject

Lists jobs paths


235
236
237
238
239
240
241
# File 'lib/clean/support.rb', line 235

def jobs_in_file
  dir = Rails.root.join('app', 'jobs')
  list = Dir.glob(dir.join('**', '*.rb')).collect do |h|
    Pathname.new(h).relative_path_from(dir).to_s[0..-4]
  end
  list
end

.look_for_labels(*paths) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/clean/support.rb', line 112

def look_for_labels(*paths)
  list = []
  for path in paths.flatten
    for file in Dir.glob(path)
      source = File.read(file)
      source.gsub(/(\'[^\']+\'|\"[^\"]+\"|\:\w+)\.(tl|th)/) do |exp|
        exp.gsub!(/\.tl\z/, '')
        exp.gsub!(/\A\:/, '')
        exp = exp[1..-2] if exp =~ /\A\'.*\'\z/ || exp =~ /\A\".*\"\z/
        exp.gsub!(/\#\{[^\}]+\}/, '*')
        list << exp
      end
      source.gsub(/(\'labels\.[^\']+\'|\"labels\.[^\"]+\")\.t/) do |exp|
        exp.gsub!(/\.t\z/, '')
        exp = exp[1..-2] if exp =~ /\A\'.*\'\z/ || exp =~ /\A\".*\"\z/
        exp.gsub!(/\Alabels\./, '')
        exp.gsub!(/\#\{[^\}]+\}/, '*')
        list << exp
      end
      source.gsub(/(tg|tl|field_set|cell|cobble|subheading)\s*\(?\s*(\:?\'[^\w+\.]+\'|\:?\"[^\"]+\"|\:\w+)\s*(\)|\,|\z|\s+do)/) do |exp|
        exp = exp.split(/[\s\(\)\:\'\"\,]+/)[1]
        exp.gsub!(/\#\{[^\}]+\}/, '*')
        list << exp
      end
    end
  end
  list += Ekylibre::Navigation.parts.collect { |p| p.index.keys }.flatten.compact.map(&:to_s)
  list += Ekylibre::Navigation.parts.map(&:name).map(&:to_s)

  # list += actions_hash.delete_if{|k,v| k == "backend/dashboards" }.values.flatten.uniq.delete_if{|a| a =~ /\Alist\_/ }
  list.delete_if { |l| l == '*' || l.underscore != l || l =~ /\n/ }.uniq.sort
end

.look_for_notifications(*paths) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/clean/support.rb', line 149

def look_for_notifications(*paths)
  list = []
  for path in paths.flatten
    for file in Dir.glob(path)
      source = File.read(file)
      source.gsub(/notify(_error|_warning|_success)?(_now)?(\(\s*|\s+)\:\w+/) do |exp|
        list << exp.split(/\:/)[1].to_sym
      end
      source.gsub(/\:\w+\.tn/) do |exp|
        list << exp[1..-4].to_sym
      end
    end
  end
  list.sort
end

.look_for_rest_actionsObject


145
146
147
# File 'lib/clean/support.rb', line 145

def look_for_rest_actions
  actions_hash.delete_if { |k, _v| k == 'backend/dashboards' }.values.flatten.uniq.delete_if { |a| a.start_with?('list_') }
end

.models_in_fileObject

Lists all models that inherits of ActiveRecord but are not system


206
207
208
209
210
211
212
213
214
# File 'lib/clean/support.rb', line 206

def models_in_file
  Dir.glob(Rails.root.join('app', 'models', '*.rb')).each { |file| require file }
  ObjectSpace
    .each_object(Class)
    .select { |klass| klass < ActiveRecord::Base }
    .select { |x| !x.name.start_with?('ActiveRecord::') && !x.abstract_class? && !x.name.start_with?('HABTM_') }
    .uniq
    .sort { |a, b| a.name <=> b.name }
end

.set_search_path!Object


4
5
6
# File 'lib/clean/support.rb', line 4

def set_search_path!
  Ekylibre::Tenant.reset_search_path!
end

.text_found?(exp, *paths) ⇒ Boolean

Returns:

  • (Boolean)

165
166
167
168
169
170
171
172
# File 'lib/clean/support.rb', line 165

def text_found?(exp, *paths)
  for path in paths
    for file in Dir.glob(path)
      return true if File.read(file) =~ exp
    end
  end
  false
end

.yaml_to_hash(filename) ⇒ Object


19
20
21
22
# File 'lib/clean/support.rb', line 19

def yaml_to_hash(filename)
  hash = YAML.load(IO.read(filename).gsub(/^(\s*)no:(.*)$/, '\1__no_is_not__false__:\2'))
  deep_symbolize_keys(hash)
end

.yaml_value(value, depth = 0) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clean/support.rb', line 62

def yaml_value(value, depth = 0)
  if value.is_a?(Array)
    '[' + value.collect { |x| yaml_value(x) }.join(', ') + ']'
  elsif value.is_a?(Symbol)
    ':' + value.to_s
  elsif value.is_a?(Hash)
    hash_to_yaml(value, depth + 1)
  elsif value.is_a?(Numeric)
    value.to_s
  else
    # "'"+value.to_s.gsub("'", "''")+"'"
    '"' + value.to_s.gsub("\u00A0", '\\_') + '"'
  end
end