Module: Joinable::ActsAsJoinableComponent::ClassMethods

Includes:
Joinable::ActsAsPermissable::ClassMethods
Defined in:
lib/joinable/acts_as_joinable_component.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Joinable::ActsAsPermissable::ClassMethods

#find_with_privacy, #permission_sql_condition, #with_permission

Class Method Details

.extended(base) ⇒ Object



46
47
48
49
# File 'lib/joinable/acts_as_joinable_component.rb', line 46

def self.extended(base)        
  base.has_one :permission_link, :as => :component, :dependent => :destroy
  base.after_create :find_joinable_and_create_permission_link
end

Instance Method Details

#with_permission_sql(user, permission, options = {}) ⇒ Object

Returns the SQL necessary to find all components for which there is no associated joinable or the user has a membership with a specific permission.

Permissions which require special handling:

  • view_* - This is a class of permissions that start with the word ‘view’. When determining if a user can view any aspect of a joinable, we also check

    if the project is open.
    
  • join_and_* - This is a class of permissions that start with the words ‘join_and_’. When determining if a user will have a certain permission

    after they join a project, we need to check the default_permission_set of the project.
    


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/joinable/acts_as_joinable_component.rb', line 61

def with_permission_sql(user, permission, options = {})
  permission = permission.to_s
  
  case user
  when String
    user_id = user
  else
    user_id = user.id
  end
    
  component_id_column = options[:id_column] || "#{table_name}.id"

  permission_without_join_and_prefix = permission.gsub('join_and_', '')
  permission_or_column = permission_without_join_and_prefix == 'view' ? "permission_links.component_view_permission" : "'#{permission_without_join_and_prefix}'"

  if permission.starts_with?('view')
    "#{no_inherited_permissions_exist_sql(component_id_column)} OR #{membership_permission_exists_sql(user_id, component_id_column, permission_or_column)} OR #{default_permission_set_permission_exists_sql(component_id_column, permission_or_column)}"
  elsif permission.starts_with?('join_and_')
    default_permission_set_permission_exists_sql(component_id_column, permission_or_column)
  else
    "#{no_inherited_permissions_exist_sql(component_id_column)} OR #{membership_permission_exists_sql(user_id, component_id_column, permission_or_column)}"
  end
end