From 064794f6a663121be66a36216ac9ef44be209278 Mon Sep 17 00:00:00 2001 From: fauno Date: Sat, 20 Apr 2019 23:25:39 -0300 Subject: [PATCH] chequear el perfil propio closes #1 --- app/controllers/piratas_controller.rb | 11 +++++++++++ app/views/piratas/_pirata.json.jbuilder | 3 +++ app/views/piratas/create.json.jbuilder | 2 +- app/views/piratas/yo.json.jbuilder | 3 +++ config/initializers/inflections.rb | 2 ++ config/routes.rb | 1 + db/migrate/20190421001841_rename_pirata.rb | 8 ++++++++ test/controllers/piratas_controller_test.rb | 14 ++++++++++++++ 8 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/views/piratas/_pirata.json.jbuilder create mode 100644 app/views/piratas/yo.json.jbuilder create mode 100644 db/migrate/20190421001841_rename_pirata.rb diff --git a/app/controllers/piratas_controller.rb b/app/controllers/piratas_controller.rb index ea6090d..c1df84e 100644 --- a/app/controllers/piratas_controller.rb +++ b/app/controllers/piratas_controller.rb @@ -2,6 +2,8 @@ # Las piratas son las que activan el Miniloom class PiratasController < ApplicationController + # Todo necesita autenticación salvo la creación de cuentas + before_action :authenticate!, except: %i[create] # POST /piratas # # Registra una cuenta @@ -19,4 +21,13 @@ class PiratasController < ApplicationController render json: { errors: @pirata.errors.messages }, status: :unprocessable_entity end + + # GET /piratas/yo + # + # Obtener el perfil propio + # + # @return [Hash] { id: @int, nick: @string, email: @string } + def yo + @pirata = current_pirata + end end diff --git a/app/views/piratas/_pirata.json.jbuilder b/app/views/piratas/_pirata.json.jbuilder new file mode 100644 index 0000000..9dbae73 --- /dev/null +++ b/app/views/piratas/_pirata.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.call(pirata, :id, :nick, :email) diff --git a/app/views/piratas/create.json.jbuilder b/app/views/piratas/create.json.jbuilder index 09b3b99..ea0ae9e 100644 --- a/app/views/piratas/create.json.jbuilder +++ b/app/views/piratas/create.json.jbuilder @@ -1,3 +1,3 @@ # frozen_string_literal: true -json.call(@pirata, :id, :nick, :email) +json.partial! @pirata, as: :pirata diff --git a/app/views/piratas/yo.json.jbuilder b/app/views/piratas/yo.json.jbuilder new file mode 100644 index 0000000..ea0ae9e --- /dev/null +++ b/app/views/piratas/yo.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! @pirata, as: :pirata diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 177631c..2a1969f 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -8,6 +8,8 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.plural 'posicion', 'posiciones' inflect.singular 'posiciones', 'posicion' + inflect.plural 'pirata', 'piratas' + inflect.singular 'piratas', 'pirata' end # These inflection rules are supported but not enabled by default: diff --git a/config/routes.rb b/config/routes.rb index eb184a6..ad12fb9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true Rails.application.routes.draw do + get '/piratas/yo', to: 'piratas#yo' # No queremos un índice de piratas resources :piratas, only: %i[create] # Podemos crear consensos pero no modificarlos diff --git a/db/migrate/20190421001841_rename_pirata.rb b/db/migrate/20190421001841_rename_pirata.rb new file mode 100644 index 0000000..11f92a9 --- /dev/null +++ b/db/migrate/20190421001841_rename_pirata.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Renombrar la tabla porque nos olvidamos de las inflexiones +class RenamePirata < ActiveRecord::Migration[5.2] + def change + rename_table :pirata, :piratas + end +end diff --git a/test/controllers/piratas_controller_test.rb b/test/controllers/piratas_controller_test.rb index 602a71d..dde6e1b 100644 --- a/test/controllers/piratas_controller_test.rb +++ b/test/controllers/piratas_controller_test.rb @@ -30,4 +30,18 @@ class PiratasControllerTest < ActionDispatch::IntegrationTest body = JSON.parse @response.body assert_equal %w[errors], body.keys end + + test 'puedo ver mi perfil' do + pirata = create :pirata + + get piratas_yo_url, as: :json, headers: { + Authorization: ActionController::HttpAuthentication::Basic + .encode_credentials(pirata.email, pirata.password) + } + + body = JSON.parse @response.body + assert_equal pirata.id, body['id'] + assert_equal pirata.email, body['email'] + assert_equal pirata.nick, body['nick'] + end end