Last active
February 29, 2016 22:03
-
-
Save mrgenixus/8055f2fda4b29d4df023 to your computer and use it in GitHub Desktop.
Acceptance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Acceptance | |
| include ActiveModel::Model | |
| include ActiveModel::Validations | |
| extend ActiveModel::Naming | |
| class Type < Struct.new(:list?, :all?, :individual?); end | |
| attr_accessor :invitation_id | |
| attr_accessor :invitation_ids | |
| attr_accessor :user_id | |
| validates_presence_of :user, :invitation_id | |
| validates_presence_of :invitation_ids, if: -> { type.list? } | |
| def type | |
| Acceptance::Type.new( | |
| invitation_id.to_s == 'list' rescue false, | |
| invitation_id.to_s == 'all' rescue false, | |
| not invitation_id.to_s.in? %w(list all) | |
| ) | |
| end | |
| def user= user | |
| @user = user | |
| @user_id = user | |
| end | |
| def user | |
| @user ||= User.first(user_id) | |
| end | |
| def invitations | |
| if type.all? | |
| user.pending_friend_requests | |
| elsif type.list? | |
| user.pending_friend_requests.where(id: invitation_ids) | |
| else | |
| [Invitiation.first(invitation_id)].compact | |
| end | |
| end | |
| def save | |
| return false unless valid? | |
| invitations.each do |invitation| | |
| user.add_friend(invitation.sender) | |
| end | |
| end | |
| end | |
| class Ignorance < Acceptance | |
| def save | |
| invitations.each do |invitation| | |
| invitation.update_attribute :ignored, true | |
| end | |
| end | |
| end | |
| #USAGE: | |
| # acceptance = Acceptance.new acceptance_params | |
| # acceptance.user = current_user | |
| # acceptance.invitiation_id = params[:invitation_id] | |
| # if acceptance.save | |
| # flash and redirect | |
| # else | |
| # errors and redirect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment