Sha256: 334379e17f54ba22afa61528e9512895b9b17c431ff747d1369789fd2a166a84

Contents?: true

Size: 1.47 KB

Versions: 22

Compression:

Stored size: 1.47 KB

Contents

module QuickScript
	module Interaction
		protected

		class << self
			def included(base)
				base.send :before_filter, :handle_params if base.respond_to? :before_filter
			end
		end

		class ScopeResponder
			def initialize
				@names = {}
			end
			def respond(scope)
				@names[scope[:name]].call
			end
			def method_missing(method_sym, *args, &block)
				@names[method_sym] = block
			end
		end

		def json_resp(data, meta, opts = {})
			meta = 200 if meta == true 
			meta = 404 if meta == false 
			opts[:data] = data
			opts[:meta] = meta
			opts.to_json
		end

		def handle_params
			@scope = {}
			@scope[:name] = params[:scope].first.to_sym if params[:scope]
			@scope[:args] = params[:scope][1..-1] if params[:scope]
			@scope[:limit] = params[:limit].to_i if params[:limit]
			@scope[:page] = params[:page].to_i if params[:page]
			@scope[:offset] = (@scope[:page] - 1) * @scope[:limit] if params[:page] && params[:limit]
		end

		def get_scoped_items(model, scope, limit, offset)
				@items = model
				scope.each do |m, args|
					# validate scope
					next unless can? m.to_sym, model
					args = [current_user.id.to_s] if m.include?("my_") && current_user
					if args == []
						@items = @items.send(m)
					else
						@items = @items.send(m, *args)
					end
				end
				return [] if @items == model
				@items = @items.limit(limit).offset(offset)
		end

		def respond_to_scope(&block)
			responder = ScopeResponder.new
			block.call(responder)
			responder.respond @scope
		end
	end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
quick_script-0.10.2 lib/quick_script/interaction.rb
quick_script-0.10.1 lib/quick_script/interaction.rb
quick_script-0.10.0 lib/quick_script/interaction.rb
quick_script-0.9.10 lib/quick_script/interaction.rb
quick_script-0.9.9 lib/quick_script/interaction.rb
quick_script-0.9.8 lib/quick_script/interaction.rb
quick_script-0.9.7 lib/quick_script/interaction.rb
quick_script-0.9.6 lib/quick_script/interaction.rb
quick_script-0.9.5 lib/quick_script/interaction.rb
quick_script-0.9.3 lib/quick_script/interaction.rb
quick_script-0.9.2 lib/quick_script/interaction.rb
quick_script-0.9.1 lib/quick_script/interaction.rb
quick_script-0.9.0 lib/quick_script/interaction.rb
quick_script-0.8.0 lib/quick_script/interaction.rb
quick_script-0.7.0 lib/quick_script/interaction.rb
quick_script-0.5.14 lib/quick_script/interaction.rb
quick_script-0.5.12 lib/quick_script/interaction.rb
quick_script-0.5.11 lib/quick_script/interaction.rb
quick_script-0.5.10 lib/quick_script/interaction.rb
quick_script-0.5.9 lib/quick_script/interaction.rb