Class: SimpleShowHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_show_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(_object, _options) ⇒ SimpleShowHelper

Returns a new instance of SimpleShowHelper.



115
116
117
118
119
120
121
# File 'lib/simple_show_helper.rb', line 115

def initialize(_object, _options)
  @object = _object
  @options = _options
  @table_class = _options[:table_class] || "table table-striped table-bordered"

  scan_object
end

Instance Method Details

#humanize(o) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/simple_show_helper.rb', line 2

def humanize(o)
  return case o.class.to_s
           when "Time", "ActiveSupport::TimeWithZone" then
             I18n.l(o, format: :long)
           when "TrueClass" then
             I18n.t(:true)
           when "FalseClass" then
             I18n.t(:no)
           when "Hash" then
             d = Array.new
             o.keys.sort.each do |k|
               d << [k, humanize(o[k])]
             end
             render_table(d, @table_class)
           when "Array" then
             render_table(o.collect { |a| [humanize(a)] }, @table_class)
           when NilClass
             '...'
           else
             o.to_s
         end

end

#render_table(data, table_class) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simple_show_helper.rb', line 26

def render_table(data, table_class)
  s = "<table class=\"#{table_class}\">"
  data.each do |dat|
    s += "<tr>\n"
    dat.each do |d|
      s += "<td>#{d}</td>\n"
    end
    s += "</tr>\n"
  end
  s += "</table>"

  return s
end

#scan_objectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
111
112
113
# File 'lib/simple_show_helper.rb', line 40

def scan_object
  # column used to describe association
  association_desc_keys = ["name", "email", "desc", "type", "id"]
  special_attrs = ["id", "created_at", "updated_at"]
  object_tableize = @object.class.to_s.tableize.singularize

  # foreign keys, beware! magic!
  assoc_keys = @object.class.reflect_on_all_associations.collect { |a| a.foreign_key }.uniq & @object.attributes.keys
  # associations classes
  associations = Hash.new
  assoc_keys.each do |ak|
    associations[ak] = @object.class.reflect_on_all_associations.select { |a| a.foreign_key == ak }.first.klass
  end

  if @options[:attrs]
    attrs = @options[:attrs].collect { |k| k.to_s }
  else
    # remove special attributes
    attrs = @object.attributes.keys.sort
    special_attrs.each do |s|
      attrs.delete(s)
    end
  end

  # model's regular attributes
  details = Array.new
  attrs.each do |d|
    # key = I18n.t("#{object_tableize}.#{d}")
    key = @object.class.human_attribute_name(d)
    value = @object.attributes[d]


    if assoc_keys.include?(d)
      # nobody wants id
      value = ""

      r = associations[d].where(id: value.to_i).first
      if r
        can_read = can?(:read, r)


        association_desc_keys.each do |ak|
          if r.attributes[ak]
            value = r.attributes[ak]
            break
          end
        end

        # if can read, link to it
        if can_read
          value = "<a href=\"#{url_for(r)}\">#{value}</a>"
        end
      end

    end

    #details << [, m.attributes[d]]
    # http://apidock.com/rails/ActiveModel/Translation/human_attribute_name
    details << [key, value]
  end

  # timestamps
  if @options[:timestamps]
    #details << [m.class.human_attribute_name("created_at"), l(m.attributes["created_at"], format: :long)]
    #details << [m.class.human_attribute_name("updated_at"), l(m.attributes["updated_at"], format: :long)]
    details << [I18n.t("created_at"), @object.attributes["created_at"]]
    details << [I18n.t("updated_at"), @object.attributes["updated_at"]]
  end

  @details = Array.new
  details.each do |d|
    @details << [humanize(d[0]), humanize(d[1])]
  end
end

#to_sObject



123
124
125
126
# File 'lib/simple_show_helper.rb', line 123

def to_s
  @s = render_table(@details, @table_class)
  return @s
end