Reduces code duplication in ApiControllers.

Methods for controller name changes in before filters (for permission
checks) now generated dynamically, class methods moved to their own
module.
pull/175/head
Martin Czuchra 12 years ago
parent 39598a1b82
commit e0fb8460f4
  1. 83
      app/controllers/api/v1/api_controller.rb
  2. 80
      app/controllers/api/v2/api_controller.rb

@ -14,64 +14,47 @@ module Api
module ApiController
def self.included(base)
base.class_eval do
skip_before_filter :disable_api
prepend_before_filter :disable_everything_except_api
module ClassMethods
def included(base)
base.class_eval do
if ((respond_to? :skip_before_filter) &&
(respond_to? :prepend_before_filter))
skip_before_filter :disable_api
prepend_before_filter :disable_everything_except_api
end
end
end
end
def api_version
@@api_version ||= /api\/v1\//
end
def check_if_deletion_allowed(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def find_project(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
def permeate_permissions(*filter_names)
filter_names.each do |filter_name|
define_method filter_name do |*args, &block|
begin
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
ensure
params[:controller] = original_controller
end
result
end
end
end
end
def find_time_entry(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
extend ClassMethods
def find_optional_project(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
def api_version
/api\/v1\//
end
def authorize_for_user(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
permeate_permissions :authorize
permeate_permissions :authorize_for_user
permeate_permissions :check_if_deletion_allowed
permeate_permissions :find_optional_project
permeate_permissions :find_project
permeate_permissions :find_time_entry
def authorize(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
end
end
end

@ -14,82 +14,18 @@ module Api
module ApiController
extend ::Api::V1::ApiController
def self.included(base)
base.class_eval do
skip_before_filter :disable_api
prepend_before_filter :disable_everything_except_api
end
end
include ::Api::V1::ApiController
extend ::Api::V1::ApiController::ClassMethods
def api_version
@@api_version ||= /api\/v2\//
/api\/v2\//
end
def authorize(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def determine_base(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def apply_at_timestamp(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def jump_to_project_menu_item(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def find_project(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def find_all_projects_by_project_id(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def find_project_by_project_id(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
def authorize(*args, &block)
original_controller = params[:controller]
params[:controller] = original_controller.gsub(api_version, "")
result = super(*args, &block)
params[:controller] = original_controller
result
end
permeate_permissions :apply_at_timestamp,
:determine_base,
:find_all_projects_by_project_id,
:find_project_by_project_id,
:jump_to_project_menu_item
end
end

Loading…
Cancel
Save