Skip to content

Instantly share code, notes, and snippets.

@randalmaile
Last active January 2, 2016 13:09
Show Gist options
  • Save randalmaile/8307982 to your computer and use it in GitHub Desktop.
Save randalmaile/8307982 to your computer and use it in GitHub Desktop.
Bloccit - Another Interlude
1. In the up_vote? and down_vote? methods, how is value not nil? don't you have to call self.value?
## Here are some things we missed:
1. We should redirect users to the topics view after they sign-in;
2. The current password field should be removed from registration/edit so that users signing-in via Facebook can update their information without entering a password;
3. If a user has previously voted, we should change the up/down arrow icon to reflect that they have voted;
4. We'll take a look at updating a couple of default bootstrap colors;
5. We need to check that we have the proper permissions before sending emails based on favorited posts.
###Removing Password Requirements -To simplify the Edit User form (remove the password confirm field) and to remove password fields from FaceBook authenticated users
1. In the class RegistrationsController < Devise::RegistrationsController controller, we need to create a new controller (and adjust the routes) called controllers/users/registrations_controller.rb add the following code according to the Devise wiki page:
```
# required for settings form to submit when password is left blank
if account_update_params[:password].blank?
account_update_params.delete("password")
account_update_params.delete("password_confirmation")
end
```
... the code we're adding is:
```
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
```
...the difference is that we....
a.
b. routes.rb: add registrations: 'users/registrations' to the hash passed to devise_for :users, controllers:
2. Kill the password fields in the devise/registrations/edit.html.haml file and add:
```
<% unless current_user.provider? %>
<div class="control-group">
<%= f.label :password, class: 'control-label' %>
<div class="controls">
<%= f.password_field :password %>
</div>
</div>
<% end %>
```
... this is great because you've only got one condition (unless) that, when false (i.e. - there is no provider, i.e., you are not being authenticated through Facebook, then you get the password field, otherwise, it's not shown
3. Change the opacity on either the up or down arrow - to indicate the current user has voted or not...
a. set CSS opacity - .voted class
b. Create a method in the user.rb model - pass in the post and return a vote (either a valid instance or nil)
- def voted(post)
self.votes.where(post_id: post.id).first
end
c. Adjust the voter partial to evaluate that the vote's from the current user and it was either up_vote? or down_vote? and attach the .voted class - user ternary operator
d. create the up_vote? and down_vote? methods in the vote.rb model:
- def up_vote?
value == 1
end
def down_vote?
value == -1
end
4. Checking user Permissions before emailing - remember when we added the email_favorites: boolean column to the to :users table and created an accessible attribute?
```
if favorite.user_id != self.user_id && favorite.user.email_favorites? # translates to: "evaluate to true if the favorited post does not have your own comments and the email_favorites attribute is set to true (or checked in the user profile checkbox)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment