class ItemsController

Public Instance Methods

create() click to toggle source
# File app/controllers/items_controller.rb, line 14
def create
  # directory = Directory.find(params[:directory_id])
  @item = Item.new(**item_params)
  groups.each do |group|
    @item.add group, authorization_user: current_user
  end
  respond_to do |format|
    if @item.new_record? && @item.save
      format.js
    else
      format.js { render json: @item.errors.full_messages.uniq , status: :unprocessable_entity }
    end
  end    
end
destroy() click to toggle source
# File app/controllers/items_controller.rb, line 89
def destroy
  @item = Item.find(params[:id])
  respond_to do |format|
    if @item && @item.destroy
      format.js
    else
      format.js { render json: @item.errors.full_messages.uniq , status: :unprocessable_entity }
    end
  end
end
edit() click to toggle source
# File app/controllers/items_controller.rb, line 29
def edit
  @item = Item.find(params[:id])
  @item.authorize current_user
  @directory = @item.directory
  @groups = @item.groups
  respond_to do |format|
    format.js
  end
end
new() click to toggle source
# File app/controllers/items_controller.rb, line 4
def new
  @item=Item.new
  @directory=Directory.find(params[:parent_id])
  @groups = @directory.groups 
  respond_to do |format|
    format.js
    format.html { render partial: 'form', locals: {modal: false, directory: @directory, item: @item, groups: @groups, action: :create } }
  end
end
update() click to toggle source
# File app/controllers/items_controller.rb, line 39
def update
  @item = Item.find(params[:id])
  @item.authorize current_user
  # TODO: should modify only the users groups, not touch the others
  # @item.groups.delete(group)
  # @item.groups = []
  # groups.each do |group|
  #   @item.add group
  # end

  item_groups_previous = @item.groups.to_a
  errors = nil; error = false
  Item.transaction do 
    if groups.empty?
      errors = "can't be empty"
    else
      @item.update_attributes item_params
      groups_to_add = (groups - item_groups_previous) & current_user.groups
      logger.debug "***** to add: #{(groups_to_add)}"
      groups_to_add.each do |group|
        @item.add group
      end
      logger.debug "***** afer add: #{@item.groups.to_a}"
      unless @item.save
        error = true
        logger.debug "INSIDE *********** -------------------- #{errors}, error: #{error}"
        raise ActiveRecord::Rollback 
      else
        groups_to_destroy = (item_groups_previous - groups) & current_user.groups
        logger.debug "***** to destroy: #{groups_to_destroy}"
        @item.groups.destroy groups_to_destroy
        logger.debug "***** afer destroy: #{@item.groups.to_a}"
        if @item.groups.empty?
          errors = "can't be empty"
          raise ActiveRecord::Rollback 
        end
      end
    end
  end
  @item.errors[:groups] << errors if errors
  logger.debug "*********** -------------------- #{errors}, error: #{error}"
  respond_to do |format|
    if !error && errors.blank? && @item.save 
      format.js
    else
      format.js { render json: @item.errors.full_messages.uniq , status: :unprocessable_entity }
    end
  end
end