Class: SimpleTeams::MembershipForm

Inherits:
ApplicationForm
  • Object
show all
Defined in:
app/forms/simple_teams/membership_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(membership, current_user) ⇒ MembershipForm

Returns a new instance of MembershipForm.



6
7
8
9
10
11
12
# File 'app/forms/simple_teams/membership_form.rb', line 6

def initialize(membership, current_user)
  @membership = membership
  @current_user = current_user

  # Get existing attributes
  @role = @membership.role
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



4
5
6
# File 'app/forms/simple_teams/membership_form.rb', line 4

def current_user
  @current_user
end

#membershipObject

Returns the value of attribute membership.



4
5
6
# File 'app/forms/simple_teams/membership_form.rb', line 4

def membership
  @membership
end

#roleObject

Returns the value of attribute role.



4
5
6
# File 'app/forms/simple_teams/membership_form.rb', line 4

def role
  @role
end

Instance Method Details

#available_rolesObject



34
35
36
# File 'app/forms/simple_teams/membership_form.rb', line 34

def available_roles
  SimpleTeams::Membership.roles
end

#perform(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'app/forms/simple_teams/membership_form.rb', line 23

def perform(params)
  self.assign_attributes(params)

  if valid?
    membership.update(params)
    generate_notification
  else
    false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'app/forms/simple_teams/membership_form.rb', line 14

def valid?
  self.errors.clear
  validate_role_is_available
  validate_current_user_has_rights_to_modify_an_existing_owner
  validate_current_user_has_rights_to_add_an_owner
  validate_not_last_owner
  !self.errors.present?
end