Skip to content

Commit

Permalink
Merge pull request #43 from jeduardo824/chore/enum-for-feelings
Browse files Browse the repository at this point in the history
Chore/enum for feelings
  • Loading branch information
marclerodrigues authored Dec 29, 2019
2 parents a87d40b + 40528cb commit b206b30
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 32 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Entry < ApplicationRecord
extend Enumerize

belongs_to :user

enumerize :feeling, in: { angry: 0, sad: 1, confused: 2, happy: 3 }
end
10 changes: 5 additions & 5 deletions app/views/entries/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
<% end %>

<div class="field">
<%= form.label :feeling %>
<%= form.text_field :feeling %>
<%= form.label t(:feeling, scope: 'activerecord.attributes.entries') %>
<%= form.select :feeling, Entry.feeling.options %>
</div>

<div class="field">
<%= form.label :description %>
<%= form.label t(:description, scope: 'activerecord.attributes.entries') %>
<%= form.text_area :description %>
</div>

<div class="field">
<%= form.label :hour %>
<%= form.label t(:hour, scope: 'activerecord.attributes.entries') %>
<%= form.time_field :hour %>
</div>

<div class="field">
<%= form.label :day %>
<%= form.label t(:day, scope: 'activerecord.attributes.entries') %>
<%= form.date_field :day %>
</div>

Expand Down
20 changes: 10 additions & 10 deletions app/views/entries/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<p id="notice"><%= notice %></p>
<div class="container">
<h1 class="title">Your Entries</h1>
<h1 class="title"><%= t(:entry, scope: 'activerecord.models').pluralize %></h1>

<table class="table">
<thead>
<tr class="columnTitle">
<th>Feeling</th>
<th>Description</th>
<th>Hour</th>
<th>Day</th>
<th><%= t(:feeling, scope: 'activerecord.attributes.entries') %></th>
<th><%= t(:description, scope: 'activerecord.attributes.entries') %></th>
<th><%= t(:hour, scope: 'activerecord.attributes.entries') %></th>
<th><%= t(:day, scope: 'activerecord.attributes.entries') %></th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @entries.each do |entry|%>
<tr class="entrie">
<td class="column"><%= entry.feeling %></td>
<td class="column"><%= t(entry.feeling, scope: 'activerecord.attributes.entries.feelings') %></td>
<td class="column"><%= entry.description %></td>
<td class="column"><%= entry.hour.try(:strftime, "%I:%M %p") || "-" %></td>
<td class="column"><%= entry.day || "-" %></td>
<td class="column"><%= link_to 'Show', entry %></td>
<td class="column"><%= link_to 'Edit', edit_entry_path(entry) %></td>
<td class="column"><%= link_to 'Destroy', entry, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td class="column"><%= link_to t(:show), entry %></td>
<td class="column"><%= link_to t(:edit), edit_entry_path(entry) %></td>
<td class="column"><%= link_to t(:delete), entry, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to "How are you feeling today?", new_entry_path, class: "newEntryButton" %>
<%= link_to t(:how_are_you), new_entry_path, class: "newEntryButton" %>
</div>
14 changes: 7 additions & 7 deletions app/views/entries/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<p id="notice"><%= notice %></p>

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

<p>
<strong>Description:</strong>
<strong><%= t(:description, scope: 'activerecord.attributes.entries') %>:</strong>
<%= @entry.description %>
</p>

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

<%= 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 %>
8 changes: 8 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
24 changes: 24 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions spec/controllers/entries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/entries.rb
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
4 changes: 4 additions & 0 deletions spec/models/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/views/entries/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/views/entries/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/views/entries/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/views/entries/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -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 <p>" do
render
expect(rendered).to match(/Feeling/)
expect(rendered).to match(/Sad/)
expect(rendered).to match(/MyText/)
end
end

0 comments on commit b206b30

Please sign in to comment.