Skip to content

Commit

Permalink
Add user activity tasks to user spec and related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeckman committed Nov 6, 2014
1 parent 602ef81 commit bb37803
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 27 deletions.
6 changes: 4 additions & 2 deletions app/controllers/tasks/usage/user_activity_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class Tasks::Usage::UserActivityController < ApplicationController
include TaskControllerConfiguration

def index
@project_members = Project.find(@sessions_current_project_id).users

# @project_members = Project.find(@sessions_current_project_id).users
@project_members = User.all # Project.find(@sessions_current_project_id).users
end


def report
@user = User.find(params[:id])
end
Expand Down
16 changes: 16 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ def pinboard_hash(project_id)
pinboard_items.where(project_id: project_id).order('pinned_object_type DESC').to_a.group_by { |a| a.pinned_object_type }
end

def total_for_Otu_class
Otu.where(creator:self).count
end

def last_otu_created
Otu.limit(1).order(created_at: :desc).first
end

def total_objects(klass) # klass_name is a string, need .constantize in next line
klass.where(creator:self).count
end

def total_objects2(klass_string)
self.send("created_#{klass_string}").count # klass.where(creator:self).count
end

private

def set_remember_token
Expand Down
8 changes: 5 additions & 3 deletions app/views/tasks/usage/user_activity/report.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@


<h1> Report for <%= object_tag(@user) -%> </h1>


<% @user.class.reflect_on_all_associations(:has_many).each do |r| %>
<% if r.name =~ /created_/ %>
<div><%= @user.send(r).count %></div>
<% end %>
<% end %>
<%= link_to('select another user', user_activity_task_path) -%>
77 changes: 55 additions & 22 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
let(:user) { User.new(password: 'password',
password_confirmation: 'password',
email: 'user_model@example.com',
name: 'Bob'
)}
name: 'Bob'
) }
subject { user }

context 'associations' do
Expand All @@ -15,7 +15,7 @@
expect(user.projects << Project.new()).to be_truthy
end

specify 'pinbaord_items' do
specify 'pinboard_items' do
expect(user.pinboard_items << PinboardItem.new()).to be_truthy
end
end
Expand Down Expand Up @@ -108,79 +108,112 @@
before { user.save }
context 'password is not validated on .update() when neither password and password_confirmation are provided' do
before { user.update(email: 'abc@def.com') }
it {is_expected.to be_valid}
it { is_expected.to be_valid }
specify 'without errors' do
expect(user.errors.count).to eq(0)
end
end

context 'password is validated on .update() when password is provided' do
before { user.update(password: 'Abcd123!') }
it {is_expected.not_to be_valid}
it { is_expected.not_to be_valid }
end

context 'password is validated on .update() when password is provided' do
before { user.update(password_confirmation: 'Abcd123!') }
it {is_expected.not_to be_valid}
it { is_expected.not_to be_valid }
end
end

describe 'remember token' do
before { user.save }
it(:remember_token) { is_expected.not_to be_blank }
end

describe 'password reset token' do

it 'is nil on a newly created user' do
expect(user.password_reset_token).to be_nil
end

describe '#generate_password_reset_token' do
it 'records the time it was generated' do
Timecop.freeze(DateTime.now) do
user.generate_password_reset_token()
expect(user.password_reset_token_date).to eq(DateTime.now)
Timecop.freeze(DateTime.now) do
user.generate_password_reset_token()
expect(user.password_reset_token_date).to eq(DateTime.now)
end
end

it 'generates a random token' do
expect(user.generate_password_reset_token()).to_not eq(user.generate_password_reset_token())
end

it 'does not record the token in plain text' do
token = user.generate_password_reset_token()
expect(token).to_not eq(user.password_reset_token)
end

it 'generates the token with at least 16 chars' do
expect(user.generate_password_reset_token).to satisfy { |v| v.length >= 16 }
end
end

describe '#password_reset_token_matches?' do

context 'valid' do
it 'returns truthy when the supplied token matches the user''s' do
it 'returns truthy when the supplied token matches the user' 's' do
token = user.generate_password_reset_token()
expect(user.password_reset_token_matches?(token)).to be_truthy
end
end

context 'invalid' do
let(:examples) { [nil, '', 'token'] }

it 'returns falsey when the user has no token' do
user.password_reset_token = nil
examples.each { |e| expect(user.password_reset_token_matches?(e)).to be_falsey }
end
it 'returns falsey when the supplied token does not match the user''s' do

it 'returns falsey when the supplied token does not match the user' 's' do
user.generate_password_reset_token()
examples.each { |e| expect(user.password_reset_token_matches?(e)).to be_falsey }
end
end
end
end


context 'user activity summaries' do
before {
user.save
4.times { (FactoryGirl.create(:valid_otu, creator: user, updater: user)) }
@last_otu = FactoryGirl.create(:valid_otu, creator: user, updater: user)
# some data
}

=begin
specify '.total_for_Otu_class' do
expect(user.total_for_Otu_class).to eq 5
end
=end

specify '.last Otu created by me' do
expect(user.last_otu_created).to eq @last_otu
end

specify '.total_objects(Otu)' do
expect(user.total_objects(Otu)).to eq 5
end

specify ".total_objects2('otus')" do # klass_string expects plural
expect(user.total_objects2('otus')).to eq 5
end



end


end

0 comments on commit bb37803

Please sign in to comment.