Sha256: 48b51dd6a86abd2e99ec6be57f77d0333f8bdc88042df962689232a3701892c0

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

require_dependency "red_base/api_controller"

module RedBase
  class API::V1::GroupsController < APIController
    # TODO: Use strong params
    # TODO: implement authorization

    # GET /api/v1/groups
    def index
      @groups = Group.includes(:permissions).all
      authorize! :read, @groups
      respond_with(@groups)
    end

    def create
      authorize! :create, RedBase::Group

      permissions = [];

      (params[:permissions] || []).each do |perm_string|
        perm, model = perm_string.split "|"
        permission = Permission.find_or_create_by_model_and_permission_type(model, perm)
        permissions << permission
      end

      @group = Group.create!({
                               name: params[:name],
                               permissions: permissions,
                             })
      respond_with(@group)

    end

    def show
      @group = Group.find(params[:id])
      authorize! :read, @group
      respond_with(@group)
    end

    def update

      @group = Group.find(params[:id])
      authorize! :update, @group

      permissions = [];
      (params[:permissions] || []).each do |perm_string|
        perm, model = perm_string.split "|"
        permission = Permission.find_or_create_by_model_and_permission_type(model, perm)
        permissions << permission
      end

      @group.update(:name => params[:name],
                    :permissions => permissions)
      respond_with(@group)
    end

    def destroy
      ids = params[:id].split(",")
      @groups = Group.where(:id => ids)
      authorize! :destory, @groups
      @groups.destroy_all
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
red_base-0.6.0 app/controllers/red_base/api/v1/groups_controller.rb
red_base-0.5.1 app/controllers/red_base/api/v1/groups_controller.rb
red_base-0.5.0 app/controllers/red_base/api/v1/groups_controller.rb