Sha256: 62b81d038baa0c4055b1b830fe7bd61c8692be892f4330c47d0cfe69330b0e7f

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 KB

Contents

module ThinkFeelDoDashboard
  module Concerns
    # addes necessary membership
    # associations and validations
    module Membership
      extend ActiveSupport::Concern

      # This is perfect for including functionality
      # provided by 3rd party gems, etc.
      included do
        attr_accessor :display_name

        before_validation :ensure_display_name_for_social_arms
        before_validation :only_one_active_group, on: :create

        validate :excludes_moderators
      end

      # methods added to Class itself...
      module ClassMethods
      end

      def active?
        (start_date.nil? || start_date <= Time.zone.today) &&
          (end_date.nil? || end_date >= Time.zone.today)
      end

      private

      def ensure_display_name_for_social_arms
        if invalid_display_name?
          errors.add(:display_name,
                     "is required because the arm of this intervention " \
                     "utilizes social features.")
        else
          name = display_name || participant.display_name
          participant.update_attributes(display_name: name)
        end
      end

      def excludes_moderators
        if active_group.try(:arm) &&
           !active_group.arm.social? &&
           participant.is_admin
          errors.add(:base, "moderators can't be part of this group.")
        end
      end

      def only_one_active_group
        if participant.active_membership && active?
          errors.add(:participant,
                     "can't be assigned to this group because they are " \
                     "already active.")
        end
      end

      def invalid_display_name?
        participant.active_membership &&
          active_group.try(:arm).try(:social?) &&
          participant.display_name.blank?
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
think_feel_do_dashboard-1.2.0.beta1 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.21 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.20 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.19 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.18 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.17 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.16 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.15 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.14 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.13 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.12 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.11 app/models/think_feel_do_dashboard/concerns/membership.rb
think_feel_do_dashboard-1.1.10 app/models/think_feel_do_dashboard/concerns/membership.rb