module Spree
  class Promotion
    module Rules
      class User < PromotionRule
        belongs_to :user, class_name: "::#{Spree.user_class.to_s}"

        has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser',
                                        foreign_key: :promotion_rule_id
        has_many :users, through: :promotion_rule_users, class_name: ::Spree.user_class.to_s

        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, options = {})
          users.include?(order.user)
        end

        def user_ids_string
          user_ids.join(',')
        end

        def user_ids_string=(s)
          self.user_ids = s.to_s.split(',').map(&:strip)
        end
      end
    end
  end
end

module Spree
  class Promotion
    module Rules
      class User < PromotionRule
        belongs_to :user, class_name: "::#{Spree.user_class.to_s}"

        has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser',
                                        foreign_key: :promotion_rule_id
        has_many :users, through: :promotion_rule_users, class_name: "::#{Spree.user_class.to_s}"

        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, options = {})
          users.include?(order.user)
        end

        def user_ids_string
          user_ids.join(',')
        end

        def user_ids_string=(s)
          self.user_ids = s.to_s.split(',').map(&:strip)
        end
      end
    end
  end
end

Fix wrong association class bug in User PromotionRule
Fix wrong association class bug in User PromotionRule

We have a custom User class in our app. It is unsurprisingly called "User".
The `Spree::Promotion::Rules::User` class has an association to `:users` set,
where the class name is bound to be `::Spree.user_class`. In our app, this evaluates to
`User`. Since the namespacing `::` were missing, the rule would return a collection
of instances of `Spree::Promotion::Rules::User` instead of actual users.

This concern has been addressed in the `belongs_to` association in the same file (forcing
the correct namespace by interpolating `::` in front of whatever is returned by `Spree.user_class`).
I use the same technique also for the `has_many` association.
