Commit 280a294

mo khan <mo@mokhan.ca>
2015-11-15 19:44:35
remove accepts_nested attributes and add tests.
1 parent 5837835
Changed files (6)
app
models
views
config
environments
spec
app/models/user.rb
@@ -3,7 +3,6 @@ class User < ActiveRecord::Base
   has_many :training_sessions
   has_many :exercise_sessions, through: :training_sessions
   has_one :profile
-  accepts_nested_attributes_for(:profile, update_only: true)
   USERNAME_REGEX=/\A[-a-z0-9_.]*\z/i
 
   validates :username, presence: true, format: { with: USERNAME_REGEX }, uniqueness: true
app/views/profiles/edit.html.erb
@@ -2,26 +2,26 @@
   <div class="small-12 columns text-center">
     <%= gravatar_for(@current_user, size: 128) %>
     <h1><%= @current_user.username %></h1>
-    <%= form_for(@profile, method: :patch) do |f| %>
+    <%= form_for(@profile) do |f| %>
       <fieldset>
-        <legend><%= t("profiles.edit.gender.gender") %></legend>
+        <legend><%= t(".gender.gender") %></legend>
         <%= f.radio_button(:gender, "female") %>
-        <%= f.label(:gender_female, t("profiles.edit.gender.female")) %>
+        <%= f.label(:gender_female, t(".gender.female")) %>
         <%= f.radio_button(:gender, "male") %>
-        <%= f.label(:gender_male, t("profiles.edit.gender.male")) %>
+        <%= f.label(:gender_male, t(".gender.male")) %>
         <%= f.radio_button(:gender, "other") %>
-        <%= f.label(:gender_other, t("profiles.edit.gender.other")) %>
+        <%= f.label(:gender_other, t(".gender.other")) %>
       </fieldset>
       <fieldset>
-        <legend><%= t("profiles.edit.social_tolerance.social_tolerance") %></legend>
+        <legend><%= t(".social_tolerance.social_tolerance") %></legend>
         <%= f.radio_button(:social_tolerance, "low") %>
-        <%= f.label(:social_tolerance_low, t("profiles.edit.social_tolerance.low")) %>
+        <%= f.label(:social_tolerance_low, t(".social_tolerance.low")) %>
         <%= f.radio_button(:social_tolerance, "medium") %>
-        <%= f.label(:social_tolerance_medium, t("profiles.edit.social_tolerance.medium")) %>
+        <%= f.label(:social_tolerance_medium, t(".social_tolerance.medium")) %>
         <%= f.radio_button(:social_tolerance, "high") %>
-        <%= f.label(:social_tolerance_high, t("profiles.edit.social_tolerance.high")) %>
+        <%= f.label(:social_tolerance_high, t(".social_tolerance.high")) %>
       </fieldset>
-      <%= f.submit t("profiles.edit.save"), class: "button"  %>
+      <%= f.submit t(".save"), class: "button"  %>
     <% end %>
   </div>
 </div>
config/environments/production.rb
@@ -46,7 +46,7 @@ Rails.application.configure do
 
   # Use the lowest log level to ensure availability of diagnostic information
   # when problems arise.
-  config.log_level = :debug
+  config.log_level = :warn
 
   # Prepend all log lines with the following tags.
   # config.log_tags = [ :subdomain, :uuid ]
spec/features/profiles_spec.rb
@@ -2,16 +2,16 @@ require "rails_helper"
 
 feature "Profiles", type: :feature do
   include_context "stronglifts_program"
-
-  subject { ProfilePage.new(user) }
   let(:user) { create(:user) }
 
-  before :each do
-    page.set_rack_session(user_id: user.id)
-    subject.visit_page
-  end
-
   context "when the user has not completed any workouts" do
+    subject { ProfilePage.new(user) }
+
+    before :each do
+      page.set_rack_session(user_id: user.id)
+      subject.visit_page
+    end
+
     it "displays the users username" do
       expect(page).to have_content(user.username)
     end
@@ -21,4 +21,22 @@ feature "Profiles", type: :feature do
       expect(page).to have_content(translations)
     end
   end
+
+  context "editing my profile" do
+    subject { EditProfilePage.new(user) }
+
+    before :each do
+      page.set_rack_session(user_id: user.id)
+      subject.visit_page
+    end
+
+    it "allows me to edit my profile" do
+      subject.change(gender: :male, social_tolerance: :low)
+
+      expect(page).to have_content(user.username)
+      expect(page).to have_content(
+        I18n.translate("profiles.edit.profile_update_success")
+      )
+    end
+  end
 end
spec/support/pages/edit_profile_page.rb
@@ -0,0 +1,15 @@
+require_relative "../page_model.rb"
+
+class EditProfilePage < PageModel
+  def initialize(user)
+    super edit_profile_path(user)
+  end
+
+  def change(gender: :male, social_tolerance: :low)
+    within(".edit_profile") do
+      page.choose(gender.to_s.titleize)
+      page.choose(social_tolerance.to_s.titleize)
+      click_button translate("profiles.edit.save")
+    end
+  end
+end
spec/support/page_model.rb
@@ -22,4 +22,10 @@ class PageModel
       login_page.login_with(username, password)
     end
   end
+
+  private
+
+  def translate(key)
+    I18n.translate(key)
+  end
 end