Class: ExpressTemplates::Components::TableFor::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/express_templates/components/table_for.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Column

Returns a new instance of Column.



113
114
115
116
117
118
119
# File 'lib/express_templates/components/table_for.rb', line 113

def initialize(name, options = {})
  @name = name
  @options = options
  @formatter = options[:formatter]
  @header = options[:header]
  @actions = options[:actions] || []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



112
113
114
# File 'lib/express_templates/components/table_for.rb', line 112

def name
  @name
end

Instance Method Details

#format(item_name) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/express_templates/components/table_for.rb', line 125

def format(item_name)
  if @formatter.nil?
    "\#\{#{item_name}.#{name}\}"
  elsif @formatter.kind_of?(Proc)
    "\#\{(#{@formatter.source}).call(#{item_name}.#{name})\}"
  end
end

#has_actions?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/express_templates/components/table_for.rb', line 121

def has_actions?
  @actions.any?
end

#show_actions(item_name) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/express_templates/components/table_for.rb', line 133

def show_actions(item_name)
  action_links = StringIO.new
  @actions.each do |action|
    action_name = action.to_s
    if action_name.eql?('edit')
      action_links.puts "<a href='/#{item_name}/{{#{item_name.singularize}.id}}/edit'>Edit</a>"
    elsif action_name.eql?('delete')
      action_links.puts "<a href='/#{item_name}/{{#{item_name.singularize}.id}}' data-method='delete' data-confirm='Are you sure?'>Delete</a>"
    elsif action_name.eql?('show')
      action_links.puts "<a href='/#{item_name}/{{#{item_name.singularize}.id}}'>Show</a>"
    end
  end
  action_links.string
end

#titleObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/express_templates/components/table_for.rb', line 148

def title
  case
  when @header.nil?
    @name.to_s.try(:titleize)
  when @header.kind_of?(String)
    @header
  when @header.kind_of?(Proc)
    "{{(#{@header.source}).call(#{@name})}}"
  end
end