Module: Monatomic::Helper

Defined in:
lib/monatomic/helper.rb

Instance Method Summary collapse

Instance Method Details

#app_nameObject



78
79
80
# File 'lib/monatomic/helper.rb', line 78

def app_name
  t :app_name, exception_handler: proc { settings.app_name }
end

#current_userObject



13
14
15
# File 'lib/monatomic/helper.rb', line 13

def current_user
  @current_user ||= (User.where(id: session[:uid]).first if session[:uid])
end

#h(str) ⇒ Object



9
10
11
# File 'lib/monatomic/helper.rb', line 9

def h str
  Rack::Utils.escape_html str
end

#path_for(*arguments) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/monatomic/helper.rb', line 58

def path_for(*arguments)
  options = arguments.last
  if options.is_a? Hash
    arguments.pop
    options.reject! { |k, v| v.blank? }
  else
    options = {}
  end
  [:sort, :search].each do |e|
    options[e] ||= params[e] if params[e]
  end
  arguments.unshift(@model.name.tableize) if @model
  base = "/" + arguments.join("/")
  if format = options.delete(:format)
    base << ".#{format}"
  end
  base << "?" + Rack::Utils.build_query(options) if options.present?
  base
end

#present(resource, field, target: :presenter) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/monatomic/helper.rb', line 17

def present resource, field, target: :presenter
  ability = {
    presenter: :readable,
    editor: :writable,
    xlsx: :readable,
  }[target]
  raise ArgumentError if ability.nil?
  if resource.method("#{ability}?").call current_user, field
    presenter = field.options[target] || field.options[:display_type]
    if field.is_a? Mongoid::Fields::ForeignKey
      value = resource.send(field.name.sub(/_id\Z/, ""))
    else
      value = resource.send(field.name)
    end
    if target == :xlsx
      if value.respond_to? :display_name
        return value.display_name
      else
        return value
      end
    end
    scope = OpenStruct.new(
      value: value,
      field: field,
      param_name: "data[#{field.name}]"
    )
    scope.define_singleton_method(:method_missing, &method(:send))
    if presenter.is_a? Symbol
      erb :"#{target}s/#{presenter}", scope: scope
    elsif presenter.is_a? String
      erb presenter, scope: scope
    elsif presenter.is_a? Proc
      scope.instance_exec(&presenter)
    else
      h "Unknown presenter #{presenter} with #{field.inspect}"
    end
  else
    t :hidden
  end
end

#send_xlsxObject



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
111
112
# File 'lib/monatomic/helper.rb', line 82

def send_xlsx
  workbook = RubyXL::Workbook.new
  workbook.cell_xfs << RubyXL::XF.new(num_fmt_id: 22)
  workbook.cell_xfs << RubyXL::XF.new(num_fmt_id: 14)
  worksheet = workbook[0]
  @fields.each_with_index do |field, i|
    worksheet.add_cell(0, i, t(field))
  end
  width = []
  @resources.each_with_index do |res, i|
    @fields.each_with_index do |field, j|
      width[j] ||= []
      v = present(res, field, target: :xlsx)
      c = worksheet.add_cell(i+1, j, v)
      if v.class.in? [Date, DateTime, Time]
        c.raw_value = workbook.date_to_num(v.to_datetime).to_s
        c.style_index = v.is_a?(Date) ? 2 : 1
        c.datatype = nil
        width[j] << (v.is_a?(Date) ? 8 : 12)
      else
        width[j] << v.to_s.length
      end
    end
  end
  width.each_with_index do |ws, i|
    worksheet.change_column_width(i, ws.max + 4)
  end
  tmp = Tempfile.new(["export", ".xlsx"])
  workbook.write tmp.path
  send_file tmp.path, filename: "#{@model.display_name}#{Time.now.to_s(:number)}.xlsx"
end

#t(*args) ⇒ Object



5
6
7
# File 'lib/monatomic/helper.rb', line 5

def t *args
  I18n.t(*args)
end