Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit ec64f9b

Browse files
author
Bill Heaton
committed
Store MD5 hash of commenter email for using with avatar
1 parent 89e4695 commit ec64f9b

8 files changed

Lines changed: 77 additions & 4 deletions

File tree

app/controllers/api/auth_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def authenticate_commenter
1717
commenter = Commenter.create!(name: params[:username], email: params[:email])
1818
end
1919
if commenter
20-
render json: { auth_token: commenter.generate_auth_token }
20+
render json: { auth_token: commenter.generate_auth_token, commenter_id: commenter.id }
2121
else
2222
render json: { error: 'Invalid name or email' }, status: :unauthorized
2323
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Api::V1::CommentersController < ApiControllerController
2+
skip_before_action :set_current_user, :authenticate_request, only: [:index, :show, :show_association, :get_related_resource]
3+
4+
private
5+
6+
def set_current_user
7+
if decoded_auth_token
8+
@current_user ||= Commenter.find(decoded_auth_token[:commenter_id])
9+
unless @current_user
10+
@current_user ||= User.find(decoded_auth_token[:user_id])
11+
end
12+
end
13+
end
14+
15+
def _serializer
16+
JSONAPI::ResourceSerializer.new(resource_klass,
17+
include: @request.include,
18+
fields: @request.fields,
19+
base_url: base_url,
20+
key_formatter: key_formatter,
21+
route_formatter: route_formatter)
22+
end
23+
24+
def commenter_params
25+
params.require(:post).permit(:name, :email)
26+
end
27+
28+
def _default_sort
29+
[{:field=>"created_at", :direction=>:desc}]
30+
end
31+
end

app/models/commenter.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
require 'digest'
2+
13
class Commenter < ActiveRecord::Base
4+
before_save :create_hash
5+
26
has_many :comments
37

48
validates :name, :length => { :minimum => 4 }
59
validates :email, uniqueness: true, email: true
10+
validates :email, uniqueness: true, email: true
611

712
def generate_auth_token
813
payload = { commenter_id: self.id }
914
AuthToken.encode(payload)
1015
end
1116

17+
def create_hash
18+
md5 = Digest::MD5.new
19+
md5 << self.email
20+
self.email_hash = md5.hexdigest
21+
end
22+
1223
def self.find_by_email(email)
1324
self.where(email: email).first
1425
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'jsonapi/resource'
22

33
class Api::V1::CommentResource < JSONAPI::Resource
4-
attributes :id, :body, :approved
4+
attributes :id, :body, :approved, :created_at
55
has_one :commenter
66
has_one :post
77
end

app/resources/api/v1/commenter_resource.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22

33
class Api::V1::CommenterResource < JSONAPI::Resource
44
attributes :id, :name
5+
attribute :hash
56
has_many :comments
7+
8+
def hash
9+
@model.email_hash
10+
end
611
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddEmailHashToCommenters < ActiveRecord::Migration
2+
def change
3+
add_column :commenters, :email_hash, :string
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20150510045633) do
14+
ActiveRecord::Schema.define(version: 20150528222934) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -28,6 +28,7 @@
2828
t.string "email"
2929
t.datetime "created_at", null: false
3030
t.datetime "updated_at", null: false
31+
t.string "email_hash"
3132
end
3233

3334
add_index "commenters", ["email"], name: "index_commenters_on_email", unique: true, using: :btree

spec/models/commenter_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
require 'rails_helper'
2+
require 'digest'
23

34
RSpec.describe Commenter, type: :model do
4-
pending "add some examples to (or delete) #{__FILE__}"
5+
6+
email = 'solo@rebels.com'
7+
8+
before(:all) do
9+
@commenter = Commenter.create({ :name => 'hanssolo', :email => email });
10+
end
11+
12+
after(:all) do
13+
@commenter.delete
14+
end
15+
16+
it 'creates a hash of the email address' do
17+
expect( @commenter ).to be_truthy
18+
expect( @commenter.email_hash ).to be_truthy
19+
20+
md5 = Digest::MD5.new
21+
md5 << email
22+
23+
expect( @commenter.email_hash ).to eq md5.hexdigest
24+
end
525
end

0 commit comments

Comments
 (0)