Class: AdminWidgets::ListWidget

Inherits:
BaseWidget
  • Object
show all
Defined in:
lib/admin_widgets/list_widget.rb

Defined Under Namespace

Classes: Field, Header

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseWidget

#capture, #content_block, #helper, #method_missing, #root

Methods included from Memoization

#memoize

Methods included from Delegation

#delegate

Constructor Details

#initialize(*attrs) ⇒ ListWidget

Returns a new instance of ListWidget.



41
42
43
44
45
# File 'lib/admin_widgets/list_widget.rb', line 41

def initialize(*attrs)
  super(*attrs)

  @fields = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AdminWidgets::BaseWidget

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/admin_widgets/list_widget.rb', line 5

def fields
  @fields
end

#table_classObject

Returns the value of attribute table_class.



4
5
6
# File 'lib/admin_widgets/list_widget.rb', line 4

def table_class
  @table_class
end

Instance Method Details

#bodyObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/admin_widgets/list_widget.rb', line 79

def body
  tbody do
    scoped_collection.each do |object|
      tr :class => row_class, :'data-id' => object.id do
        td :class => 'first'
        fields.each do |field|
          td field.value_for(object)
        end
        td :class => 'last' do
          row_actions_for(object) if row_actions
        end
      end
    end
  end
end

#contentObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/admin_widgets/list_widget.rb', line 47

def content
  content_block
  div :class => 'table-wrapper' do
    div :class => 'table-controls'
    div :class => 'table-content' do
      table :class => table_class, :id => table_id, :'data-highlight-id' => helper.flash[:highlight_id] do
        header
        body
      end
    end
  end
  pagination
end

#field(name, options = {}, &block) ⇒ Object

DSL methods



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
# File 'lib/admin_widgets/list_widget.rb', line 114

def field(name, options = {}, &block)

  value_callback = lambda do |name, options|
    attribute_path = name.to_s.split('.')
    value = attribute_path.inject(self) {|r, v| r.try(v) rescue '' }

    # FIXME: This is bad code and I should feel bad. Extract it to a presenter maybe?
    if value.is_a? Time
      I18n.l(value, :format => :short)
    elsif value.is_a? Date
      I18n.l(value, :format => :default)
    elsif value.is_a? Array
      value.join(', ')
    elsif options[:i18n]
      I18n.t(value, :scope => options[:i18n])
    else
      value
    end
  end

  @fields << Field.new(self, name, options, value_callback)

  # Haml support hack
  ''
end

#headerObject

Content methods



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/admin_widgets/list_widget.rb', line 64

def header
  thead do
    tr do
      th :class => 'first'
      fields.each do |field|
        th :class => th_class, :'data-field-name' => field.name do
          header = Header.new(self, field)
          rawtext(column_sorting ? helper.link_to(header.label, header.url, :class => ['sortable', header.css_class]) : header.label)
        end
      end
      th :class => 'last'
    end
  end
end

#paginationObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/admin_widgets/list_widget.rb', line 95

def pagination
  div :class => pagination_class do
    div :class => 'results' do
      count = [scoped_collection.count(true), scoped_collection.total_count].join(I18n.t('of', :scope => 'views.pagination'))
      label = helper.t('views.pagination.results')
      rawtext "#{label} #{count}"
    end
    rawtext helper.paginate scoped_collection, :outer_window => 1, :inner_window => 5, :params => { :search => nil }, :param_name => page_param
  end
end

#row_actions_for(object) ⇒ Object



106
107
108
109
# File 'lib/admin_widgets/list_widget.rb', line 106

def row_actions_for(object)
  rawtext helper.link_to(label = helper.t('resources.actions.edit'), edit_resource_path(object), :class => 'edit', :alt => label)
  rawtext helper.link_to(label = helper.t('resources.actions.destroy'), resource_path(object), :method => :delete, :confirm => helper.t('resources.destroy.confirm'), :class => 'destroy', :alt => label)
end