Skip to content

Commit facaad6

Browse files
committed
First commit
Adding code samples from blog post. Basic Gemfile and Rakefile
1 parent 2b12b83 commit facaad6

15 files changed

+216
-0
lines changed

1-value-objects/rating.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Rating
2+
include Comparable
3+
4+
def self.from_cost(cost)
5+
if cost <= 2
6+
new("A")
7+
elsif cost <= 4
8+
new("B")
9+
elsif cost <= 8
10+
new("C")
11+
elsif cost <= 16
12+
new("D")
13+
else
14+
new("F")
15+
end
16+
end
17+
18+
def initialize(letter)
19+
@letter = letter
20+
end
21+
22+
def better_than?(other)
23+
self > other
24+
end
25+
26+
def <=>(other)
27+
other.to_s <=> to_s
28+
end
29+
30+
def hash
31+
@letter.hash
32+
end
33+
34+
def eql?(other)
35+
to_s == other.to_s
36+
end
37+
38+
def to_s
39+
@letter.to_s
40+
end
41+
end
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class SessionsController < ApplicationController
2+
def create
3+
user = User.where(email: params[:email]).first
4+
5+
if UserAuthenticator.new(user).authenticate(params[:password])
6+
self.current_user = user
7+
redirect_to dashboard_path
8+
else
9+
flash[:alert] = "Login failed."
10+
render "new"
11+
end
12+
end
13+
end
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class UserAuthenticator
2+
def initialize(user)
3+
@user = user
4+
end
5+
6+
def authenticate(unencrypted_password)
7+
return false unless @user
8+
9+
if BCrypt::Password.new(@user.password_digest) == unencrypted_password
10+
@user
11+
else
12+
false
13+
end
14+
end
15+
end

3-form-objects/signup.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Signup
2+
include Virtus
3+
4+
extend ActiveModel::Naming
5+
include ActiveModel::Conversion
6+
include ActiveModel::Validations
7+
8+
attr_reader :user
9+
attr_reader :company
10+
11+
attribute :name, String
12+
attribute :company_name, String
13+
attribute :email, String
14+
15+
validates :email, presence: true
16+
# … more validations …
17+
18+
# Forms are never themselves persisted
19+
def persisted?
20+
false
21+
end
22+
23+
def save
24+
if valid?
25+
persist!
26+
true
27+
else
28+
false
29+
end
30+
end
31+
32+
private
33+
34+
def persist!
35+
@company = Company.create!(name: company_name)
36+
@user = @company.users.create!(name: name, email: email)
37+
end
38+
end

3-form-objects/signups_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class SignupsController < ApplicationController
2+
def create
3+
@signup = Signup.new(params[:signup])
4+
5+
if @signup.save
6+
redirect_to dashboard_path
7+
else
8+
render "new"
9+
end
10+
end
11+
end
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class AbandonedTrialQuery
2+
def initialize(relation = Account.scoped)
3+
@relation = relation
4+
end
5+
6+
def find_each(&block)
7+
@relation.
8+
where(plan: nil, invites_count: 0).
9+
find_each(&block)
10+
end
11+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class NotifyAbandonedTrials < Job
2+
def run
3+
AbandonedTrialQuery.new.find_each do |account|
4+
account.send_offer_for_support
5+
end
6+
end
7+
end
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
old_accounts = Account.where("created_at < ?", 1.month.ago)
2+
old_abandoned_trials = AbandonedTrialQuery.new(old_accounts)

5-view-objects/donut_chart.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class DonutChart
2+
def initialize(snapshot)
3+
@snapshot = snapshot
4+
end
5+
6+
def cache_key
7+
@snapshot.id.to_s
8+
end
9+
10+
def data
11+
# pull data from @snapshot and turn it into a JSON structure
12+
end
13+
end
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class ActiveUserPolicy
2+
def initialize(user)
3+
@user = user
4+
end
5+
6+
def active?
7+
@user.email_confirmed? &&
8+
@user.last_login_at > 14.days.ago
9+
end
10+
end

7-decorators/comments_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CommentsController < ApplicationController
2+
def create
3+
@comment = FacebookCommentNotifier.new(Comment.new(params[:comment]))
4+
5+
if @comment.save
6+
redirect_to blog_path, notice: "Your comment was posted."
7+
else
8+
render "new"
9+
end
10+
end
11+
end
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class FacebookCommentNotifier
2+
def initialize(comment)
3+
@comment = comment
4+
end
5+
6+
def save
7+
@comment.save && post_to_wall
8+
end
9+
10+
private
11+
12+
def post_to_wall
13+
Facebook.post(title: @comment.title, user: @comment.author)
14+
end
15+
end

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "https://rubygems.org"
2+
3+
gem "rspec"
4+
gem "rake"

Gemfile.lock

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
diff-lcs (1.1.3)
5+
rake (10.0.3)
6+
rspec (2.12.0)
7+
rspec-core (~> 2.12.0)
8+
rspec-expectations (~> 2.12.0)
9+
rspec-mocks (~> 2.12.0)
10+
rspec-core (2.12.2)
11+
rspec-expectations (2.12.1)
12+
diff-lcs (~> 1.1.3)
13+
rspec-mocks (2.12.2)
14+
15+
PLATFORMS
16+
ruby
17+
18+
DEPENDENCIES
19+
rake
20+
rspec

Rakefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rspec/core/rake_task'
2+
3+
RSpec::Core::RakeTask.new(:spec)
4+
5+
task default: :spec

0 commit comments

Comments
 (0)