diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 2ed0470..87c87b6 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index df8aaa6..a9f00da 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index 65f6164..b2fca53 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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 diff --git a/app/models/subscribe_service.rb b/app/models/subscribe_service.rb new file mode 100644 index 0000000..0a7f8ca --- /dev/null +++ b/app/models/subscribe_service.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 27b111d..2927030 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_update_form.rb b/app/models/user_update_form.rb new file mode 100644 index 0000000..f375a38 --- /dev/null +++ b/app/models/user_update_form.rb @@ -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 diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 52d18e0..d5035e1 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -1,12 +1,12 @@