Sha256: 7f5b63b3e821a99d50cc47152a7111f80943e3461ee9841188450adec4882e8e

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

#
# Copyright 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module Backstage
  class Application < Sinatra::Base
    def self.resource(*resources)
      options = resources.pop if resources.last.is_a?(Hash)
      options ||= {}
      resources.each do |resource|
        resource = resource.to_s
        klass = "backstage/#{resource}".constantize
        view_path = options[:view_path] || resource.pluralize
        get "/#{resource.pluralize}" do
          @collection = klass.all
          if html_requested?
            haml :"#{view_path}/index"
          else
            content_type :json
            collection_to_json( @collection )
          end
        end

        get "/#{resource}/:name" do
          @object = klass.find( Util.decode_name( params[:name] ) )
          if html_requested?
            haml :"#{view_path}/show"
          else
            content_type :json
            object_to_json( @object )
          end
        end

        (options[:actions] || []).each do |action|
          post "/#{resource}/:name/#{action}" do
            object = klass.find( Util.decode_name( params[:name] ) )
            object.__send__( action )
            if html_requested?
              flash[:notice] = "'#{action}' called on #{simple_class_name( object ).humanize} #{object.name}"
              redirect_to object_path( object )
            else
              content_type :json
              object_to_json( object )
            end
          end

        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
torquebox-backstage-0.2.1 lib/resource_helpers.rb
torquebox-backstage-0.2.0 lib/resource_helpers.rb
torquebox-backstage-0.1.2 lib/resource_helpers.rb
torquebox-backstage-0.1.1 lib/resource_helpers.rb
torquebox-backstage-0.1.0 lib/resource_helpers.rb