From d210adffbcc9ccc07e912a5c8feff2315c02fc41 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 8 Dec 2019 15:03:10 -0300 Subject: [PATCH 1/6] add Enumerize gem --- Gemfile | 1 + Gemfile.lock | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 6121c48..ebe622b 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem 'webpacker', '~> 4.0' gem 'turbolinks', '~> 5' gem 'jbuilder', '~> 2.7' gem 'devise' +gem 'enumerize', '~> 2.3.1' gem 'bootsnap', '>= 1.4.2', require: false group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 0cf00e9..c7c4892 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,6 +84,8 @@ GEM warden (~> 1.2.3) diff-lcs (1.3) docile (1.3.2) + enumerize (2.3.1) + activesupport (>= 3.2) erubi (1.9.0) factory_bot (5.0.2) activesupport (>= 4.2.0) @@ -253,6 +255,7 @@ DEPENDENCIES byebug capybara devise + enumerize (~> 2.3.1) factory_bot_rails (~> 5.0.2) faker (~> 2.6.0) jbuilder (~> 2.7) From 96e8675679147d506c3cacf6d38191ca19ccedbb Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 8 Dec 2019 15:03:52 -0300 Subject: [PATCH 2/6] change Entry model to enumerize feelings --- app/models/entry.rb | 4 ++++ spec/models/entry_spec.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/app/models/entry.rb b/app/models/entry.rb index 1460619..47335f5 100644 --- a/app/models/entry.rb +++ b/app/models/entry.rb @@ -1,3 +1,7 @@ class Entry < ApplicationRecord + extend Enumerize + belongs_to :user + + enumerize :feeling, in: { angry: 0, sad: 1, confused: 2, happy: 3 } end diff --git a/spec/models/entry_spec.rb b/spec/models/entry_spec.rb index ea7d42f..218ea2e 100644 --- a/spec/models/entry_spec.rb +++ b/spec/models/entry_spec.rb @@ -4,4 +4,8 @@ describe "relations" do it { is_expected.to belong_to(:user) } end + + describe "feelings" do + it { is_expected.to enumerize(:feeling).in(angry: 0, sad: 1, confused: 2, happy: 3) } + end end From 51940f7c7d3127b3eaeb494850cba65de632e0b1 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 8 Dec 2019 15:05:42 -0300 Subject: [PATCH 3/6] change views to work with enumerized feelings and I18n --- app/views/entries/_form.html.erb | 10 +++++----- app/views/entries/index.html.erb | 20 ++++++++++---------- app/views/entries/show.html.erb | 14 +++++++------- spec/views/entries/edit.html.erb_spec.rb | 2 +- spec/views/entries/index.html.erb_spec.rb | 4 ++-- spec/views/entries/new.html.erb_spec.rb | 2 +- spec/views/entries/show.html.erb_spec.rb | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/app/views/entries/_form.html.erb b/app/views/entries/_form.html.erb index d75e675..c59ce11 100644 --- a/app/views/entries/_form.html.erb +++ b/app/views/entries/_form.html.erb @@ -12,22 +12,22 @@ <% end %>
- <%= form.label :feeling %> - <%= form.text_field :feeling %> + <%= form.label t(:feeling, scope: 'activerecord.attributes.entries') %> + <%= form.select :feeling, Entry.feeling.options %>
- <%= form.label :description %> + <%= form.label t(:description, scope: 'activerecord.attributes.entries') %> <%= form.text_area :description %>
- <%= form.label :hour %> + <%= form.label t(:hour, scope: 'activerecord.attributes.entries') %> <%= form.time_field :hour %>
- <%= form.label :day %> + <%= form.label t(:day, scope: 'activerecord.attributes.entries') %> <%= form.date_field :day %>
diff --git a/app/views/entries/index.html.erb b/app/views/entries/index.html.erb index 0e96a40..9df929c 100644 --- a/app/views/entries/index.html.erb +++ b/app/views/entries/index.html.erb @@ -1,14 +1,14 @@

<%= notice %>

-

Your Entries

+

<%= t(:entry, scope: 'activerecord.models').pluralize %>

- - - - + + + + @@ -16,16 +16,16 @@ <% @entries.each do |entry|%> - + - - - + + + <% end %>
FeelingDescriptionHourDay<%= t(:feeling, scope: 'activerecord.attributes.entries') %><%= t(:description, scope: 'activerecord.attributes.entries') %><%= t(:hour, scope: 'activerecord.attributes.entries') %><%= t(:day, scope: 'activerecord.attributes.entries') %>
<%= entry.feeling %><%= t(entry.feeling, scope: 'activerecord.attributes.entries.feelings') %> <%= entry.description %> <%= entry.hour.try(:strftime, "%I:%M %p") || "-" %> <%= entry.day || "-" %><%= link_to 'Show', entry %><%= link_to 'Edit', edit_entry_path(entry) %><%= link_to 'Destroy', entry, method: :delete, data: { confirm: 'Are you sure?' } %><%= link_to t(:show), entry %><%= link_to t(:edit), edit_entry_path(entry) %><%= link_to t(:delete), entry, method: :delete, data: { confirm: 'Are you sure?' } %>
- <%= link_to "How are you feeling today?", new_entry_path, class: "newEntryButton" %> + <%= link_to t(:how_are_you), new_entry_path, class: "newEntryButton" %>
diff --git a/app/views/entries/show.html.erb b/app/views/entries/show.html.erb index 66ea03f..3f88195 100644 --- a/app/views/entries/show.html.erb +++ b/app/views/entries/show.html.erb @@ -1,23 +1,23 @@

<%= notice %>

- Feeling: - <%= @entry.feeling %> + <%= t(:feeling, scope: 'activerecord.attributes.entries') %>: + <%= t(@entry.feeling, scope: 'activerecord.attributes.entries.feelings') %>

- Description: + <%= t(:description, scope: 'activerecord.attributes.entries') %>: <%= @entry.description %>

- Hour: + <%= t(:hour, scope: 'activerecord.attributes.entries') %>: <%= @entry.hour.strftime("%I:%M %p")%>

- Day: + <%= t(:day, scope: 'activerecord.attributes.entries') %>: <%= @entry.day %>

-<%= link_to 'Edit', edit_entry_path(@entry) %> | -<%= link_to 'Back', entries_path %> +<%= link_to t(:edit), edit_entry_path(@entry) %> | +<%= link_to t(:back), entries_path %> diff --git a/spec/views/entries/edit.html.erb_spec.rb b/spec/views/entries/edit.html.erb_spec.rb index 7ebaf09..4ed2718 100644 --- a/spec/views/entries/edit.html.erb_spec.rb +++ b/spec/views/entries/edit.html.erb_spec.rb @@ -10,7 +10,7 @@ assert_select "form[action=?][method=?]", entry_path(@entry), "post" do - assert_select "input[name=?]", "entry[feeling]" + assert_select "select[name=?]", "entry[feeling]" assert_select "textarea[name=?]", "entry[description]" end diff --git a/spec/views/entries/index.html.erb_spec.rb b/spec/views/entries/index.html.erb_spec.rb index bdb9a76..5b97aae 100644 --- a/spec/views/entries/index.html.erb_spec.rb +++ b/spec/views/entries/index.html.erb_spec.rb @@ -9,9 +9,9 @@ it "renders a list of entries" do render - assert_select "tr>td", :text => entries[0].feeling + assert_select "tr>td", :text => entries[0].feeling.capitalize assert_select "tr>td", :text => entries[0].description - assert_select "tr>td", :text => entries[1].feeling + assert_select "tr>td", :text => entries[1].feeling.capitalize assert_select "tr>td", :text => entries[1].description end end diff --git a/spec/views/entries/new.html.erb_spec.rb b/spec/views/entries/new.html.erb_spec.rb index fa3320f..8bdf967 100644 --- a/spec/views/entries/new.html.erb_spec.rb +++ b/spec/views/entries/new.html.erb_spec.rb @@ -10,7 +10,7 @@ assert_select "form[action=?][method=?]", entries_path, "post" do - assert_select "input[name=?]", "entry[feeling]" + assert_select "select[name=?]", "entry[feeling]" assert_select "textarea[name=?]", "entry[description]" end diff --git a/spec/views/entries/show.html.erb_spec.rb b/spec/views/entries/show.html.erb_spec.rb index ac423f2..680358e 100644 --- a/spec/views/entries/show.html.erb_spec.rb +++ b/spec/views/entries/show.html.erb_spec.rb @@ -1,14 +1,14 @@ require 'rails_helper' RSpec.describe "entries/show", type: :view do - let(:entry) { create(:entry, feeling: 'Feeling', description: 'MyText') } + let(:entry) { create(:entry, feeling: :sad, description: 'MyText') } before(:each) do @entry = assign(:entry, entry) end it "renders attributes in

" do render - expect(rendered).to match(/Feeling/) + expect(rendered).to match(/Sad/) expect(rendered).to match(/MyText/) end end From 2d1fd78d5249d28bde627499d7492d8887e1a344 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 8 Dec 2019 15:06:04 -0300 Subject: [PATCH 4/6] add I18n translations and configs --- config/application.rb | 8 ++++++++ config/locales/en.yml | 24 +++++++++++++++++++++++- config/locales/pt-BR.yml | 24 ++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 config/locales/pt-BR.yml diff --git a/config/application.rb b/config/application.rb index 8ebe199..c0d7af9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -29,6 +29,14 @@ class Application < Rails::Application # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. + config.time_zone = 'Brasilia' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + config.i18n.default_locale = 'pt-BR' + + I18n.enforce_available_locales = false + # Don't generate system test files. config.generators.system_tests = nil end diff --git a/config/locales/en.yml b/config/locales/en.yml index cf9b342..fe512d4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -30,4 +30,26 @@ # available at https://guides.rubyonrails.org/i18n.html. en: - hello: "Hello world" + back: "Back" + create: "Create" + delete: "Delete" + edit: "Edit" + show: "Show" + how_are_you: "How are you feeling today?" + enumerize: + entry: + feeling: + angry: "Angry" + confused: "Confused" + happy: "Happy" + sad: "Sad" + activerecord: + models: + entry: "Entry" + user: "User" + attributes: + entries: + description: "Description" + feeling: "Feeling" + hour: "Hour" + day: "Day" diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml new file mode 100644 index 0000000..4dea5dc --- /dev/null +++ b/config/locales/pt-BR.yml @@ -0,0 +1,24 @@ +pt-BR: + back: "Voltar" + create: "Criar" + delete: "Deletar" + edit: "Editar" + show: "Visualizar" + how_are_you: "Como você está se sentindo hoje?" + enumerize: + entry: + feeling: + angry: "Raiva" + confused: "Confuso" + happy: "Feliz" + sad: "Triste" + activerecord: + models: + entry: "Entrada" + user: "Usuário" + attributes: + entries: + description: "Descrição" + feeling: "Sentimento" + hour: "Hora" + day: "Dia" \ No newline at end of file From 40528cbe591c26340637981c0e839f01ec72540a Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 8 Dec 2019 15:06:52 -0300 Subject: [PATCH 5/6] change factories and controller specs to work with enumerized feelings --- spec/controllers/entries_controller_spec.rb | 4 ++-- spec/factories/entries.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/entries_controller_spec.rb b/spec/controllers/entries_controller_spec.rb index dc8ccee..64bd5d4 100644 --- a/spec/controllers/entries_controller_spec.rb +++ b/spec/controllers/entries_controller_spec.rb @@ -125,14 +125,14 @@ describe "PUT #update" do context "with valid params" do let(:new_attributes) { - attributes_for(:entry, feeling: 'Good', user_id: user.id) + attributes_for(:entry, feeling: :sad, user_id: user.id) } let(:entry) { create(:entry, user_id: user.id) } it "updates the requested entry" do put :update, params: { id: entry.to_param, entry: new_attributes }, session: valid_session entry.reload - expect(entry.feeling).to eq('Good') + expect(entry.feeling).to eq("sad") end it "redirects to the entry" do diff --git a/spec/factories/entries.rb b/spec/factories/entries.rb index d08ef1b..a71649a 100644 --- a/spec/factories/entries.rb +++ b/spec/factories/entries.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :entry do - feeling { Faker::Lorem.words(number: 3) } + feeling { Faker::Number.between(from: 0, to: 3) } description { Faker::Lorem.words(number: 10) } hour { Time.now } day { Date.today} From 0f04aa6784d60eaa3393bb176456332fbafa0054 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2019 06:06:35 +0000 Subject: [PATCH 6/6] Bump rack from 2.0.7 to 2.0.8 Bumps [rack](https://github.com/rack/rack) from 2.0.7 to 2.0.8. - [Release notes](https://github.com/rack/rack/releases) - [Changelog](https://github.com/rack/rack/blob/master/CHANGELOG.md) - [Commits](https://github.com/rack/rack/compare/2.0.7...2.0.8) Signed-off-by: dependabot[bot] --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0cf00e9..847a6ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -129,7 +129,7 @@ GEM pry (>= 0.9.10, < 0.13.0) public_suffix (4.0.1) puma (3.12.2) - rack (2.0.7) + rack (2.0.8) rack-proxy (0.6.5) rack rack-test (1.1.0)