Module: RailsCom::ActiveHelper

Defined in:
app/helpers/rails_com/active_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_asserts(join = true, **options) ⇒ Object

return value by each keys which is true

active_asserts(active: true, item: false)
#=> 'active'


7
8
9
10
11
12
13
14
15
# File 'app/helpers/rails_com/active_helper.rb', line 7

def active_asserts(join = true, **options)
  keys = options.select { |_, v| v }.keys

  if join
    keys.join(' ')
  else
    keys.last.to_s
  end
end

#active_helper(paths: [], controllers: [], modules: [], active: nil, item: nil, **options) ⇒ Object

See examples bellow: paths:

active_helper paths: '/work/employees' or active_helper paths: ['/work/employees']

controllers:

active_helper controllers: 'xxx'  or active_helper controllers: ['xxx1', 'admin/xxx2']

modules:

active_helper modules: 'admin/oa'

action:

active_helper 'work/employee': ['index', 'show']

params:

active_helper controller: 'users', action: 'show', id: 371


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/rails_com/active_helper.rb', line 28

def active_helper(paths: [], controllers: [], modules: [], active: nil, item: nil, **options)
  check_parameters = options.delete(:check_parameters)

  if paths.present?
    Array(paths).each do |path|
      return active if current_page?(path, check_parameters: check_parameters)
    end
  end

  if controllers.present?
    return active if (Array(controllers) & [controller_name, controller_path]).size > 0
  end

  if modules.present?
    this_modules = controller_path.split('/')
    this_modules.pop
    _this_modules = []
    while this_modules.size >= 1
      this_modules.each.with_index do |_, index|
        _this_modules << this_modules[0, index + 1].join('/')
      end
      this_modules.shift
    end
    return active if (Array(modules) & _this_modules).size > 0
  end

  return active if options.present? && current_page?(options)
  return active if options.find { |key, value| [controller_name, controller_path].include?(key.to_s) && Array(value).include?(action_name) }

  item
end

#active_params(active: nil, item: nil, **options) ⇒ Object

return value by params

active_params state: 'xxx', organ_id: 1


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/rails_com/active_helper.rb', line 62

def active_params(active: nil, item: nil, **options)
  options.compact.each do |k, v|
    if params[k].to_s == v.to_s
      return active
    end

    if !params.key?(k) && session[k].to_s == v.to_s
      return active
    end
  end

  item
end

#filter_params(options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/rails_com/active_helper.rb', line 76

def filter_params(options = {})
  only = Array(options.delete(:only)).presence
  except = Array(options.delete(:except))
  query = request.GET.dup
  query.merge!(options)

  if only
    query = query.extract!(*only)
  else
    excepts = except.map(&:to_s) + ['commit', 'utf8', 'page']
    query.except!(*excepts)
  end

  if query.length > 1
    query.reject! { |_, value| value.blank? }
  else
    query.reject! { |_, value| value.nil? }
  end
  query
end