Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@ class ProjectsController < ApplicationController
def index
# FIXME: 1. Intention Revealing Method

# When user is admitted we show it's active projects
if current_user && current_user.projects.exists?
if user_is_admitted?
@projects = current_user.active_projects
else
# If not admitted we show some featured projects
@projects = Project.featured

# set a marketing flash if user is new,
# and a special price for user who signed up less than a week ago
if current_user && current_user.created_at >= 1.week.ago
flash.now[:notice] = 'Upgrade and get 20% off for having your own projects!'
elsif current_user && current_user.created_at < 1.week.ago
flash.now[:notice] = 'Upgrade for having your own projects!'
end
set_flash_for_not_admitted_user
end
end

Expand All @@ -37,16 +28,7 @@ def update
if @project.update(project_params)

# FIXME: 2 Move controller logic into model
if @project.is_featured?
if @project.created_at > 1.week.ago
@project.label = "new featured"
else
@project.label = "featured"
end
else
@project.label = "normal"
end
@project.save
@project.update_label

redirect_to @project, notice: 'Project was successfully updated.'
else
Expand Down Expand Up @@ -80,4 +62,26 @@ def set_project
def project_params
params.require(:project).permit(:title, :description, :is_featured)
end

private

def user_is_admitted?
current_user && current_user.projects.exists?
end

def set_flash_for_not_admitted_user
if new_free_user?
flash.now[:notice] = 'Upgrade and get 20% off for having your own projects!'
elsif old_free_user?
flash.now[:notice] = 'Upgrade for having your own projects!'
end
end

def new_free_user?
current_user && current_user.created_at >= 1.week.ago
end

def old_free_user?
current_user && current_user.created_at < 1.week.ago
end
end
12 changes: 7 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ def create
end

def edit
@user = User.find(params[:id])
@user.profile ||= @user.build_profile
@user_update_form = UserUpdateForm.new(params[:id])
# @user = User.find(params[:id])
# @user.profile ||= @user.build_profile
end

def update
if @user.update(user_params)
redirect_to @user, notice: 'User was successfully updated.'
@user_update_form = UserUpdateForm.new(params[:id])
if @user_update_form.update(user_params)
redirect_to user_path(@user_update_form.user), notice: 'User was successfully updated.'
else
render :edit
end
Expand All @@ -52,6 +54,6 @@ def set_user

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, profile_attributes: [:id, :nickname, :bio] )
params.require(:user_update_form).permit(:email,:id, :nickname, :bio)
end
end
13 changes: 13 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ class Project < ActiveRecord::Base

belongs_to :user

def update_label
if is_featured?
if created_at > 1.week.ago
self.label = "new featured"
else
self.label = "featured"
end
else
self.label = "normal"
end
save
end

end
30 changes: 30 additions & 0 deletions app/models/subscribe_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class SubscribeService
def initialize(user)
@user = user
end

def subscribe
api_result = try_api { PaymentGateway.subscribe }
if api_result == :success
@user.update_attributes(subscription_plan: "monthly")
end
api_result
end

def unsubscribe
api_result = try_api { PaymentGateway.unsubscribe }
if api_result == :success
@user.update_attributes(subscription_plan: nil)
end
api_result
end

private

def try_api
yield
rescue => e
Rails.logger.error "API call failed. message: #{e.message}"
return :network_error
end
end
20 changes: 5 additions & 15 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,17 @@ def active_projects

# FIXME: 4. Service Object
def subscribe
api_result = try_api { PaymentGateway.subscribe }
if api_result == :success
update_attributes(subscription_plan: "monthly")
end
api_result
subscribe_service.subscribe
end

def unsubscribe
api_result = try_api { PaymentGateway.unsubscribe }
if api_result == :success
update_attributes(subscription_plan: nil)
end
api_result
subscribe_service.unsubscribe
end

private

def try_api
yield
rescue => e
Rails.logger.error "API call failed. message: #{e.message}"
return :network_error
def subscribe_service
@subscribe_service ||= SubscribeService.new(self)
end

end
80 changes: 80 additions & 0 deletions app/models/user_update_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class UserUpdateForm
include ActiveModel::Model

attr_reader :user
attr_reader :profile

attr_accessor :email # from user
attr_accessor :nickname # from profile
attr_accessor :bio # from profile


validates_presence_of :email
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
validates_presence_of :nickname, :bio
validates_length_of :nickname, in: 3..20

def initialize(user_id)
@user = User.find_by_id(user_id)
@profile = @user.profile || @user.build_profile
end

def persisted?
@user.persisted? && @profile.persisted?
end

def update(params)
self.email = params[:email]
self.nickname = params[:nickname]
self.bio = params[:bio]
# binding.pry
if valid?
@user.update_attributes(email: email)
@profile.update_attributes(nickname: nickname, bio: bio)
true
else
false
end
end
end



# class SignupForm
# include ActiveModel::Model

# attr_reader :company
# attr_reader :user

# attr_accessor :company_name
# attr_accessor :name
# attr_accessor :email

# validates :company_name, presence: true
# validates :name, presence: true
# validates :email, presence: true
# validate :email_uniqueness

# # Forms are never themselves persisted
# def persisted?
# false
# end

# def save
# if valid?
# @company = Company.create!(name: company_name)
# @user = @company.users.create!(name: name, email: email)
# true
# else
# false
# end
# end

# private

# def email_uniqueness
# if User.where(email: email).exists?
# errors.add :email, "has already been taken"
# end
# end
# end
32 changes: 14 additions & 18 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<h1>Editing user</h1>

<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<%= form_for(@user_update_form, url: user_path(@user_update_form.user)) do |f| %>
<% if @user_update_form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<h2><%= pluralize(@user_update_form.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% @user.errors.full_messages.each do |message| %>
<% @user_update_form.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
Expand All @@ -18,24 +18,20 @@
<%= f.text_field :email %>
</div>

<%= f.fields_for :profile, @user.profile do |ff| %>
<h2>Profile</h2>

<h2>Profile</h2>

<div class="field">
<%= ff.label :nickname %><br>
<%= ff.text_field :nickname %>
</div>

<div class="field">
<%= ff.label :bio %><br>
<%= ff.text_area :bio, rows: 5, cols: 40 %>
</div>
<div class="field">
<%= f.label :nickname %><br>
<%= f.text_field :nickname %>
</div>

<% end %>
<div class="field">
<%= f.label :bio %><br>
<%= f.text_area :bio, rows: 5, cols: 40 %>
</div>

<div class="actions">
<%= f.submit %>
<%= f.submit "Update User" %>
</div>
<% end %>

Expand Down
Loading