diff --git a/app/controllers/washout_builder/washout_builder_controller.rb b/app/controllers/washout_builder/washout_builder_controller.rb index 22cd4e1..7155fb6 100644 --- a/app/controllers/washout_builder/washout_builder_controller.rb +++ b/app/controllers/washout_builder/washout_builder_controller.rb @@ -5,7 +5,7 @@ class WashoutBuilder::WashoutBuilderController < ActionController::Base def all route = params[:name].present? ? controller_is_a_service?(params[:name]) : nil if route.present? - @document = initialize_service_generator(route) + @document = WashoutBuilder::Document::Generator.new(route.defaults[:controller]) render :template => "wash_with_html/doc", :layout => false, :content_type => 'text/html' else @@ -17,16 +17,6 @@ def all private - def initialize_service_generator(route) - controller_class_name = controller_class(route.defaults[:controller]) - WashoutBuilder::Document::Generator.new( - :config => controller_class_name.soap_config, - :service_class => controller_class_name, - :soap_actions => controller_class_name.soap_actions - ) - end - - def all_services @map_controllers = map_controllers @services = @map_controllers.blank? ? [] : @map_controllers.map do |controller_name| diff --git a/lib/washout_builder.rb b/lib/washout_builder.rb index ef6fd07..d13e339 100644 --- a/lib/washout_builder.rb +++ b/lib/washout_builder.rb @@ -14,7 +14,7 @@ Virtus::InstanceMethods::Constructor.class_eval do alias_method :original_initialize,:initialize def initialize(attributes = nil) - if self.class.ancestors.detect{ |fault| WashoutBuilder::Type.get_fault_classes.include?(fault) }.present? || WashoutBuilder::Type.get_fault_classes.include?(self.class) + if WashoutBuilder::Type.valid_fault_class?(self.class) attributes = {:message => attributes} unless attributes.is_a?(Hash) end original_initialize(attributes) diff --git a/lib/washout_builder/document/complex_type.rb b/lib/washout_builder/document/complex_type.rb index adbcfb0..2fbca63 100644 --- a/lib/washout_builder/document/complex_type.rb +++ b/lib/washout_builder/document/complex_type.rb @@ -42,7 +42,7 @@ def remove_type_inheritable_elements( keys) def fix_descendant_wash_out_type(config, complex_class) param_class = complex_class.is_a?(Class) ? complex_class : complex_class.constantize rescue nil - if !param_class.nil? && param_class.ancestors.include?(WashOut::Type) && !map[0].nil? + if param_class.present? && param_class.ancestors.include?(WashOut::Type) && map[0].present? descendant = WashOut::Param.parse_builder_def(config, param_class.wash_out_param_map)[0] self.name = descendant.name self.map = descendant.map diff --git a/lib/washout_builder/document/generator.rb b/lib/washout_builder/document/generator.rb index 781291a..9fca5de 100644 --- a/lib/washout_builder/document/generator.rb +++ b/lib/washout_builder/document/generator.rb @@ -1,29 +1,34 @@ +require_relative "./virtus_model" module WashoutBuilder module Document class Generator - @attrs = [:soap_actions, :config, :service_class] + @attrs = [:soap_actions, :config, :controller_name] attr_reader *@attrs attr_accessor *@attrs - def initialize(attrs = {}) - self.config = attrs[:config] - self.service_class = attrs[:service_class] - self.soap_actions = attrs[:soap_actions] + def initialize(controller) + controller_class_name = controller_class(controller) + self.config =controller_class_name.soap_config + self.soap_actions = controller_class_name.soap_actions + self.controller_name = controller end def namespace - config.namespace + config.respond_to?(:namespace) ? config.namespace : nil end - + def controller_class(controller) + "#{controller}_controller".camelize.constantize + end + def endpoint - namespace.gsub("/wsdl", "/action") + namespace.blank? ? nil : namespace.gsub("/wsdl", "/action") end def service - service_class.name.underscore.gsub("_controller", "").camelize + controller_name.blank? ? nil : controller_name.camelize end def service_description @@ -35,7 +40,7 @@ def operations end def sort_complex_types(types, type) - types.sort_by { |hash| hash[type.to_sym].to_s.downcase }.uniq {|hash| hash[type.to_sym] } unless types.blank? + types.sort_by { |hash| hash[type.to_sym].to_s.downcase }.uniq {|hash| hash[type.to_sym] } unless types.blank? end @@ -75,35 +80,24 @@ def complex_types def actions_with_exceptions - soap_actions.select{|operation, formats| !formats[:raises].blank? } + soap_actions.select{|operation, formats| !formats[:raises].blank? } end def exceptions_raised - actions_with_exceptions.collect {|operation, formats| formats[:raises].is_a?(Array) ? formats[:raises] : [formats[:raises]] }.flatten - end - - def fault_classes - WashoutBuilder::Type.get_fault_classes - end - - def has_ancestor_fault?(fault_class) - fault_class.ancestors.detect{ |fault| fault_classes.include?(fault) }.present? - end - - def valid_fault_class?(fault) - fault.is_a?(Class) && ( has_ancestor_fault?(fault) || fault_classes.include?(fault)) + actions_with_exceptions.collect {|operation, formats| formats[:raises].is_a?(Array) ? formats[:raises] : [formats[:raises]] }.flatten end - + + def filter_exceptions_raised - exceptions_raised.select { |x| valid_fault_class?(x) } unless actions_with_exceptions.blank? + exceptions_raised.select { |x| WashoutBuilder::Type.valid_fault_class?(x) } unless actions_with_exceptions.blank? end def get_complex_fault_types(fault_types) defined = filter_exceptions_raised if defined.blank? - defined = [fault_classes.first] + defined = [WashoutBuilder::Type.get_fault_classes.first] else - defined << fault_classes.first + defined << WashoutBuilder::Type.get_fault_classes.first end defined.each{ |exception_class| exception_class.get_fault_class_ancestors( fault_types, true)} unless defined.blank? fault_types @@ -112,7 +106,7 @@ def get_complex_fault_types(fault_types) def fault_types fault_types = get_complex_fault_types([]) complex_types = extract_nested_complex_types_from_exceptions(fault_types) - complex_types.delete_if{ |hash| fault_types << hash if valid_fault_class?(hash[:fault]) } unless complex_types.blank? + complex_types.delete_if{ |hash| fault_types << hash if WashoutBuilder::Type.valid_fault_class?(hash[:fault]) } unless complex_types.blank? fault_types = sort_complex_types(fault_types, "fault") complex_types = sort_complex_types(complex_types, "fault") [fault_types, complex_types] @@ -123,20 +117,12 @@ def extract_nested_complex_types_from_exceptions(fault_types) fault_types.each do |hash| hash[:structure].each do |attribute, attr_details| complex_class = hash[:fault].get_virtus_member_type_primitive(attr_details) - unless complex_class.nil? - param_class = complex_class.is_a?(Class) ? complex_class : complex_class.constantize rescue nil - if !param_class.nil? && param_class.ancestors.include?(Virtus::Model::Core) - param_class.send :extend, WashoutBuilder::Document::VirtusModel - param_class.get_fault_class_ancestors( complex_types) - elsif !param_class.nil? && !param_class.ancestors.include?(Virtus::Model::Core) - raise RuntimeError, "Non-existent use of `#{param_class}` type name or this class does not use Virtus.model. Consider using classified types that include Virtus.mode for exception atribute types." - end - end - end + WashoutBuilder::Document::VirtusModel.extract_nested_complex_types(complex_class, complex_types) + end end complex_types end - + diff --git a/lib/washout_builder/document/virtus_model.rb b/lib/washout_builder/document/virtus_model.rb index 1f77208..b885f28 100644 --- a/lib/washout_builder/document/virtus_model.rb +++ b/lib/washout_builder/document/virtus_model.rb @@ -62,6 +62,18 @@ def get_virtus_model_structure end + def self.extract_nested_complex_types(complex_class, complex_types) + unless complex_class.nil? + param_class = complex_class.is_a?(Class) ? complex_class : complex_class.constantize rescue nil + if param_class.present? && param_class.ancestors.include?(Virtus::Model::Core) + param_class.send :extend, WashoutBuilder::Document::VirtusModel + param_class.get_fault_class_ancestors( complex_types) + elsif param_class.present? && !param_class.ancestors.include?(Virtus::Model::Core) + raise RuntimeError, "Non-existent use of `#{param_class}` type name or this class does not use Virtus.model. Consider using classified types that include Virtus.mode for exception atribute types." + end + end + end + end end end diff --git a/lib/washout_builder/type.rb b/lib/washout_builder/type.rb index b4ad96f..5ee0ded 100644 --- a/lib/washout_builder/type.rb +++ b/lib/washout_builder/type.rb @@ -21,6 +21,15 @@ def self.get_fault_classes return faults end + + def self.has_ancestor_fault?(fault_class) + fault_class.ancestors.detect{ |fault| get_fault_classes.include?(fault) }.present? + end + + def self.valid_fault_class?(fault) + fault.is_a?(Class) && ( has_ancestor_fault?(fault) || get_fault_classes.include?(fault)) + end + end end diff --git a/lib/washout_builder/version.rb b/lib/washout_builder/version.rb index aae821e..d2f354c 100644 --- a/lib/washout_builder/version.rb +++ b/lib/washout_builder/version.rb @@ -1,3 +1,3 @@ module WashoutBuilder - VERSION = "0.12.2" + VERSION = "0.12.3" end