mirror of
https://github.com/kbparagua/paloma
synced 2023-03-27 23:21:17 -04:00
Move methods to other units
This commit is contained in:
parent
ae4c012182
commit
b4d2341326
4 changed files with 102 additions and 58 deletions
|
@ -7,5 +7,7 @@ end
|
|||
# TODO: Rails version checking
|
||||
|
||||
require 'action_controller/railtie'
|
||||
require 'paloma/controller'
|
||||
require 'paloma/utilities'
|
||||
require 'paloma/action_controller_extension'
|
||||
require 'paloma/rails/engine'
|
||||
|
|
|
@ -61,53 +61,21 @@ module Paloma
|
|||
#
|
||||
#
|
||||
def js path_or_options, options = {:params => {}, :only => {}, :except => {}}
|
||||
options ||= {}
|
||||
|
||||
# js false, options
|
||||
stop = self.try_paloma_stop(path_or_options, options) if !path_or_options
|
||||
return if stop
|
||||
return if !path_or_options && self.paloma.attempt_clear_request!(options)
|
||||
|
||||
self.paloma.params.merge! options[:params] || {}
|
||||
|
||||
if path_or_options.is_a? String
|
||||
path = path_or_options.split '#'
|
||||
resource = path.first
|
||||
action = path.length != 1 ? path.last : nil
|
||||
|
||||
@__paloma_request[:resource] = resource unless resource.blank?
|
||||
@__paloma_request[:action] = action unless action.blank?
|
||||
route = ::Paloma::Utilities.interpret_route path_or_options
|
||||
self.paloma.resource = route[:resource] unless route[:resource].blank?
|
||||
self.paloma.action = route[:action] unless route[:action].blank?
|
||||
|
||||
elsif path_or_options.is_a? Symbol
|
||||
@__paloma_request[:action] = path_or_options
|
||||
self.paloma.action = path_or_options
|
||||
|
||||
elsif path_or_options.is_a? Hash
|
||||
self.set_paloma_params path_or_options[:params]
|
||||
self.paloma.params.merge! path_or_options[:params] || {}
|
||||
end
|
||||
|
||||
self.set_paloma_params options[:params] || {}
|
||||
end
|
||||
|
||||
|
||||
|
||||
def try_paloma_stop path_or_options, options = {}
|
||||
current_action = @__paloma_request[:action]
|
||||
valid_action = true
|
||||
|
||||
if options[:only].present?
|
||||
valid_action = options[:only].include?(current_action.to_sym) ||
|
||||
options[:only].include?(current_action.to_s)
|
||||
end
|
||||
|
||||
if options[:except].present?
|
||||
valid_action = !options[:except].include?(current_action.to_sym) ||
|
||||
!options[:except].include?(current_action.to_s)
|
||||
end
|
||||
|
||||
if valid_action
|
||||
@__paloma_request = nil
|
||||
return true
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
|
||||
|
@ -117,15 +85,8 @@ module Paloma
|
|||
# Keeps track of what Rails controller/action is executed.
|
||||
#
|
||||
def track_paloma_request
|
||||
resource = controller_path.split('/').map(&:titleize).join('/').gsub(' ', '')
|
||||
|
||||
@__paloma_request = {:resource => resource, :action => self.action_name}
|
||||
|
||||
#
|
||||
# Apply controller wide settings if any
|
||||
#
|
||||
return if self.class.paloma_settings.nil?
|
||||
self.js self.class.paloma_settings[:path_or_options], self.class.paloma_settings[:params]
|
||||
self.paloma.resource = ::Paloma::Utilities.get_resource controller_path
|
||||
self.paloma.action = self.action_name
|
||||
end
|
||||
|
||||
|
||||
|
@ -137,11 +98,11 @@ module Paloma
|
|||
# will execute the tracked Paloma requests.
|
||||
#
|
||||
def append_paloma_hook
|
||||
return true if @__paloma_request.nil?
|
||||
return true if self.paloma.has_no_request?
|
||||
|
||||
hook = view_context.render(
|
||||
:partial => 'paloma/hook',
|
||||
:locals => {:request => @__paloma_request})
|
||||
:locals => {:request => self.paloma.request})
|
||||
|
||||
before_body_end_index = response_body[0].rindex('</body>')
|
||||
|
||||
|
@ -156,17 +117,11 @@ module Paloma
|
|||
response.body += hook
|
||||
end
|
||||
|
||||
@__paloma_request = nil
|
||||
self.paloma.clear_request
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def set_paloma_params params
|
||||
@__paloma_request[:params] ||= {}
|
||||
@__paloma_request[:params].merge! params
|
||||
end
|
||||
|
||||
|
||||
def html_is_rendered?
|
||||
not_redirect = self.status != 302
|
||||
[nil, 'text/html'].include?(response.content_type) && not_redirect
|
||||
|
@ -178,11 +133,19 @@ module Paloma
|
|||
#
|
||||
def render options = nil, extra_options = {}, &block
|
||||
[:json, :js, :xml, :file].each do |format|
|
||||
js false if options.has_key?(format)
|
||||
self.paloma.clear_request if options.has_key?(format)
|
||||
end if options.is_a?(Hash)
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def paloma
|
||||
@paloma ||= ::Paloma::Controller.new
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
|
61
lib/paloma/controller.rb
Normal file
61
lib/paloma/controller.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
module Paloma
|
||||
class Controller
|
||||
|
||||
attr_accessor :resource, :action, :params
|
||||
|
||||
|
||||
|
||||
def initialize
|
||||
self.clear_request
|
||||
end
|
||||
|
||||
|
||||
def clear_request
|
||||
self.resource = nil
|
||||
self.action = nil
|
||||
self.params = {}
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
|
||||
def attempt_clear_request! options = {:only => {}, :except => {}}
|
||||
return self.clear_request if options.blank?
|
||||
|
||||
clear = true
|
||||
only = options[:only]
|
||||
except = options[:except]
|
||||
|
||||
if only.present?
|
||||
clear = only.include?(self.action.to_s) ||
|
||||
only.include?(self.action.to_sym)
|
||||
end
|
||||
|
||||
if except.present?
|
||||
clear = except.include?(self.action.to_s) ||
|
||||
except.include?(self.action.to_sym)
|
||||
end
|
||||
|
||||
return self.clear_request if clear
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
|
||||
def request
|
||||
{:resource => self.resource, :action => self.action, :params => self.params}
|
||||
end
|
||||
|
||||
|
||||
def has_request?
|
||||
self.resource.present? && self.action.present?
|
||||
end
|
||||
|
||||
|
||||
def has_no_request?
|
||||
!self.has_request?
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
18
lib/paloma/utilities.rb
Normal file
18
lib/paloma/utilities.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
module Paloma
|
||||
class Utilities
|
||||
|
||||
def self.get_resource controller_path
|
||||
controller_path.split('/').map(&:titleize).join('/').gsub(' ', '')
|
||||
end
|
||||
|
||||
|
||||
def self.interpret_route route_string
|
||||
parts = route_string.split '#'
|
||||
resource = parts.first
|
||||
action = parts.length != 1 ? parts.last : nil
|
||||
|
||||
{:resource => resource, :action => action}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue