lib/plutonium/resource/controller.rb in plutonium-0.15.7 vs lib/plutonium/resource/controller.rb in plutonium-0.15.8

- old
+ new

@@ -32,11 +32,11 @@ end # Gets the resource class for the controller # @return [ActiveRecord::Base] The resource class def resource_class - return @resource_class if @resource_class.present? + return @resource_class if @resource_class name.to_s.gsub(/^#{current_package}::/, "").gsub(/Controller$/, "").classify.constantize rescue NameError raise NameError, "Failed to determine the resource class. Please call `controller_for(MyResource)` in #{name}." end @@ -50,11 +50,11 @@ end # Returns the resource record based on path parameters # @return [ActiveRecord::Base, nil] The resource record def resource_record - @resource_record ||= current_authorized_scope.from_path_param(params[:id]).first! if params[:id].present? + @resource_record ||= current_authorized_scope.from_path_param(params[:id]).first! if params[:id] @resource_record end # Returns the submitted resource parameters # @return [Hash] The submitted resource parameters @@ -123,23 +123,27 @@ end # Applies submitted resource params if they have been passed def maybe_apply_submitted_resource_params! ensure_get_request - resource_record.attributes = submitted_resource_params if params[resource_param_key].present? + resource_record.attributes = submitted_resource_params if params[resource_param_key] end # Returns the current parent based on path parameters # @return [ActiveRecord::Base, nil] The current parent def current_parent - return unless parent_route_param.present? + return unless parent_route_param @current_parent ||= begin parent_route_key = parent_route_param.to_s.gsub(/_id$/, "").to_sym parent_class = current_engine.resource_register.route_key_lookup[parent_route_key] - parent_scope = parent_class.from_path_param(params[parent_route_param]) - parent_scope = parent_scope.associated_with(current_scoped_entity) if scoped_to_entity? + parent_scope = if scoped_to_entity? + parent_class.associated_with(current_scoped_entity) + else + parent_class.all + end + parent_scope = parent_scope.from_path_param(params[parent_route_param]) current_parent = parent_scope.first! authorize! current_parent, to: :read? current_parent end end @@ -151,11 +155,11 @@ end # Returns the parent input parameter # @return [Symbol, nil] The parent input parameter def parent_input_param - return unless current_parent.present? + return unless current_parent resource_class.reflect_on_all_associations(:belongs_to).find { |assoc| assoc.klass.name == current_parent.class.name }&.name&.to_sym end # Ensures the method is a GET request @@ -180,10 +184,10 @@ end # Overrides parent parameters # @param [Hash] input_params The input parameters def override_parent_params(input_params) - if current_parent.present? + if current_parent if input_params.key?(parent_input_param) || resource_class.method_defined?(:"#{parent_input_param}=") input_params[parent_input_param] = current_parent end if input_params.key?(:"#{parent_input_param}_id") || resource_class.method_defined?(:"#{parent_input_param}_id=")