module RearActions

Public Instance Methods

delete_delete_selected() click to toggle source
# File lib/rear/actions.rb, line 82
def delete_delete_selected
  halt(400, '%s is in readonly mode' % model) if readonly?
  if (ids = params[:items].to_s.split.map(&:to_i).select {|r| r > 0}).any?
    orm.delete_multiple(*ids)
  end
end
delete_reverse_assoc(source_ctrl, assoc_type, assoc_name, item_id, attached = nil) click to toggle source
# File lib/rear/actions.rb, line 61
def delete_reverse_assoc source_ctrl, assoc_type, assoc_name, item_id, attached = nil
  case @reverse_assoc.type
  when :belongs_to, :has_one
    @reverse_assoc.source_item.send '%s=' % @reverse_assoc.name, nil
  when :has_many
    if sequel?
      (@reverse_assoc.source_item.send(@reverse_assoc.name)||[]).each do |ti|
        next unless ti[pkey] == @reverse_assoc.target_item[pkey]
        m = 'remove_' << orm.singularize(@reverse_assoc.name)
        @reverse_assoc.source_item.send m, ti
      end
    else
      target_items = Array.new(@reverse_assoc.source_item.send(@reverse_assoc.name)||[]).reject do |ti|
        ti[pkey] == @reverse_assoc.target_item[pkey]
      end
      @reverse_assoc.source_item.send '%s=' % @reverse_assoc.name, target_items
    end
  end
  @reverse_assoc.source_item.save
end
get_assets(*) click to toggle source
# File lib/rear/actions.rb, line 93
def get_assets(*)
  env['PATH_INFO'] = env['PATH_INFO'].to_s.sub(ASSETS__SUFFIX_REGEXP, '')
  send_files @__rear__assets_fullpath ? @__rear__assets_fullpath :
    (@__rear__assets_path ? File.join(app.root, @__rear__assets_path) : ASSETS__PATH)
end
get_bulk_edit() click to toggle source
# File lib/rear/actions.rb, line 16
def get_bulk_edit
  @items = params[:items].to_s.gsub(/[^\d|\s]/, '')
  self.item, self.item_id = model.new, 0
  reander_p 'editor/bulk_edit'
end
get_edit(id) click to toggle source
# File lib/rear/actions.rb, line 12
def get_edit id
  reander_l(:layout) { reander_p 'editor/layout' }
end
get_index() click to toggle source
# File lib/rear/actions.rb, line 4
def get_index
  reander_l(:layout) { reander_p 'pane/layout' }
end
get_quickview() click to toggle source
# File lib/rear/actions.rb, line 8
def get_quickview
  reander_p 'pane/quickview'
end
get_reverse_assoc(source_ctrl, assoc_type, assoc_name, item_id, attached = nil) click to toggle source
# File lib/rear/actions.rb, line 39
def get_reverse_assoc source_ctrl, assoc_type, assoc_name, item_id, attached = nil
  reander_p 'pane/assocs'
end
html_filters(partial = false) click to toggle source
# File lib/rear/actions.rb, line 89
def html_filters partial = false
  partial ? render_filters : reander_p('filters/layout')
end
post_bulk_edit() click to toggle source
# File lib/rear/actions.rb, line 22
def post_bulk_edit
  data = Hash[params]
  (columns = data.delete('rear-bulk_editor-crudifier_toggler')).is_a?(Array) ||
    halt(400, 'Nothing to update. Please edit at least one column.')
  items = data.delete('rear-bulk_editor-items').to_s.strip.split
  items.empty? && halt(400, 'No items selected')

  data.reject! {|k,v| !columns.include?(k)}
  updated, failed = [], []
  items.each do |id|
    status, h, body = invoke_via_put(:crud, id, data)
    status == 200 ? updated << id : failed << ['%s Failed' % id, *body]
  end
  failed.any? && styled_halt(500, failed)
  "Successfully Updated Items:\n%s" % updated.join(', ')
end
post_reverse_assoc(source_ctrl, assoc_type, assoc_name, item_id, attached = nil) click to toggle source
# File lib/rear/actions.rb, line 43
def post_reverse_assoc source_ctrl, assoc_type, assoc_name, item_id, attached = nil
  case @reverse_assoc.type
  when :belongs_to, :has_one
    @reverse_assoc.source_item.send '%s=' % @reverse_assoc.name, @reverse_assoc.target_item
  when :has_many
    @reverse_assoc.source_item.send(@reverse_assoc.name).each do |ti|
      ti[pkey] == @reverse_assoc.target_item[pkey] && halt(400, "Relation already exists")
    end
    if sequel?
      m = 'add_' << orm.singularize(@reverse_assoc.name)
      @reverse_assoc.source_item.send m, @reverse_assoc.target_item
    else
      @reverse_assoc.source_item.send(@reverse_assoc.name) << @reverse_assoc.target_item
    end
  end
  @reverse_assoc.source_item.save
end