Module: Hammock::ExportScope::ClassMethods

Defined in:
lib/hammock/export_scope.rb

Instance Method Summary collapse

Instance Method Details

#export_scope(verb) ⇒ 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
53
54
55
56
57
58
59
60
61
62
# File 'lib/hammock/export_scope.rb', line 24

def export_scope verb
  verbable = "#{verb}able"

  metaclass.instance_eval {
    # Model.verbable_by: returns all records that are verbable by account.
    define_method "#{verbable}_by" do ||
      if !.nil? && respond_to?("#{verb}_scope_for")
        select &send("#{verb}_scope_for", )
      elsif respond_to?("#{verb}_scope")
        select &send("#{verb}_scope")
      else
        log "No #{verb} scopes available."
        nil
      end
    end

    # Model.verbable: returns all records that are verbable by anonymous users.
    define_method verbable do
      send "#{verbable}_by", nil
    end
  }

  # Model#verbable_by?: returns whether this record is verbable by account.
  define_method "#{verbable}_by?" do ||
    if !.nil? && self.class.respond_to?("#{verb}_scope_for")
      self.class.send("#{verb}_scope_for", ).call(self)
    elsif self.class.respond_to?("#{verb}_scope")
      self.class.send("#{verb}_scope").call(self)
    else
      log "No #{verb} scopes available, returning false."
      false
    end
  end

  # Model#verbable?: returns whether this record is verbable by anonymous users.
  define_method "#{verbable}?" do
    send "#{verbable}_by?", nil
  end
end

#export_scopes(*verbs) ⇒ Object



20
21
22
# File 'lib/hammock/export_scope.rb', line 20

def export_scopes *verbs
  verbs.discard(:create).each {|verb| export_scope verb }
end

#public_scope_for(verb) ⇒ Object



12
13
14
# File 'lib/hammock/export_scope.rb', line 12

def public_scope_for verb
  "#{verb}able" if respond_to? "#{verb}_scope"
end

#user_scope_for(verb) ⇒ Object



16
17
18
# File 'lib/hammock/export_scope.rb', line 16

def user_scope_for verb
  "#{verb}able_by" if respond_to? "#{verb}_scope_for"
end