Sha256: cde1cedb22db229a9a9609baaaf5c55fada58f486dbac58f6636eb5316139f80

Contents?: true

Size: 1.08 KB

Versions: 33

Compression:

Stored size: 1.08 KB

Contents

module Locomotive
  class Membership

    include Locomotive::Mongoid::Document

    ROLES = %w(author designer admin)

    ## fields ##
    field :role, default: 'author'

    ## associations ##
    belongs_to  :account, class_name: 'Locomotive::Account', validate: false
    embedded_in :site,    class_name: 'Locomotive::Site',    inverse_of: :memberships

    ## validations ##
    validates_presence_of :account
    validate              :account_is_unique

    ## virtual attributes ##
    attr_accessor :email

    ## callbacks ##
    before_save :define_role

    ## methods ##

    ROLES.each do |_role|
      define_method("#{_role}?") do
        self.role == _role
      end
    end

    def to_role
      self.role.to_sym
    end

    protected

    def account_is_unique
      if self.site.memberships.where(account_id: self.account_id).count > 1
        [:email, :account].each do |attribute|
          self.errors.add(attribute, :unique_account)
        end
      end
    end

    def define_role
      self.role = ROLES.include?(role.downcase) ? role.downcase : ROLES.first
    end

  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
locomotivecms-4.0.0.rc0 app/models/locomotive/membership.rb
locomotivecms-4.0.0.alpha3 app/models/locomotive/membership.rb
locomotivecms-3.4.1 app/models/locomotive/membership.rb
locomotivecms-4.0.0.alpha2 app/models/locomotive/membership.rb
locomotivecms-4.0.0.alpha1 app/models/locomotive/membership.rb
locomotivecms-3.4.0 app/models/locomotive/membership.rb
locomotivecms-3.3.0 app/models/locomotive/membership.rb
locomotivecms-3.3.0.rc3 app/models/locomotive/membership.rb
locomotivecms-3.3.0.rc2 app/models/locomotive/membership.rb
locomotivecms-3.1.2 app/models/locomotive/membership.rb
locomotivecms-3.2.1 app/models/locomotive/membership.rb
locomotivecms-3.3.0.rc1 app/models/locomotive/membership.rb
locomotivecms-3.2.0 app/models/locomotive/membership.rb
locomotivecms-3.2.0.rc2 app/models/locomotive/membership.rb
locomotivecms-3.2.0.rc1 app/models/locomotive/membership.rb
locomotivecms-3.1.1 app/models/locomotive/membership.rb
locomotivecms-3.1.0 app/models/locomotive/membership.rb
locomotivecms-3.1.0.rc3 app/models/locomotive/membership.rb
locomotivecms-3.1.0.rc2 app/models/locomotive/membership.rb
locomotivecms-3.1.0.rc1 app/models/locomotive/membership.rb