module RespondWithService::InstanceMethods

Private Instance Methods

find_model_instance(class_name) click to toggle source
# File lib/respond_with_service.rb, line 30
def find_model_instance class_name
  # TODO model can be conf
  model = Object.const_get class_name
  case params[:action].to_sym
  when :create
    @model_instance = model.new
    @fail_path      = :new
  when :update
    @model_instance = model.find params[:id]
    @fail_path      = :edit
  end 
end
find_service_instance() click to toggle source
# File lib/respond_with_service.rb, line 43
def find_service_instance
  service = Object.const_get(@model_instance.class.to_s)
  .const_get "#{params[:action].capitalize}#{@model_instance.class}Service"
  @service_instance = service.new params, @model_instance
end
redirect_to_after_call() click to toggle source
# File lib/respond_with_service.rb, line 12
def redirect_to_after_call
  if @service_instance.call
    flash[:notice] = 'Successfully update'
    redirect_to [:admin, @model_instance.class.to_s.underscore.pluralize]
  else
    # TODO notice errors ap create_account_service.errors
    flash[:error] = 'something went wrong'
    if @fail_path == :new
      redirect_to [@fail_path, :admin, @model_instance.class.to_s.underscore]
    else
      redirect_to [@fail_path, :admin, @model_instance]
    end
    


  end
end