Module: Cms::Acts::CmsUser::MacroMethods

Defined in:
lib/cms/acts/cms_user.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#acts_as_cms_user(options = {}) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cms/acts/cms_user.rb', line 24

def acts_as_cms_user(options = {})
  include InstanceMethods

  # Adding a cms_group method to each person object that returns a list of
  # CMS Groups the user belongs to.  Can be set with an array, proc, or
  # instance method on the person.  Defaults to Cms::Group.guest
  define_method "cms_groups" do
    fetch_group_list = Proc.new do
      if options[:groups].nil?
        [Cms::Group.guest]
      elsif options[:groups].kind_of? Array
        options[:groups]
      elsif options[:groups].is_a? Proc
        options[:groups].call(self)
      elsif self.respond_to?(options[:groups])
        self.send(options[:groups])
      else
        raise ArgumentError, "acts_as_cms_user :groups option expected to be of type Array, Proc, or instance method - #{options[:groups].class} found instead"
      end
    end

    @cms_groups ||= begin
      specified = Array(fetch_group_list.call).select { |x| x.kind_of? Cms::Group }
      specified << Cms::Group.guest if specified.empty?
      specified
    end

  end
end