Module: StatusFor::StatusFor

Defined in:
lib/status_for/acts_as_statusable_for.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *subject, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/status_for/acts_as_statusable_for.rb', line 30

def method_missing(method_id, *subject, &block)
  # This creates a method for self called 'status_for(subject)' that finds all the
  # self items with the 'status_for' with the subject id.
  # Example:  Message.deleted_for(user)
  # Returns array of messages which contains user.id in the message's 'deleted_for'
  if method_id.to_s =~ /^([a-z]+)_for$/
    run_find_status_for($1, subject.first)

  # This creates a method for self called 'not_status_for(subject)' that finds all the
  # self items with the 'status_for' THAT DOES NOT HAVE the subject id.
  # Example:  Message.deleted_for(user)
  # Returns array of messages which DOES NOT contain user.id in the message's 'deleted_for'
  elsif method_id.to_s =~ /^not_([a-z]+)_for$/
    run_find_not_status_for($1, subject.first)
  else
    super
  end 
end

Instance Method Details

#initialize_status_for(subject) ⇒ Object

Step 2: In a migration, include a ‘status_for’ column in the model of interest.

This looks something like add_column :messages, :deleted_for, “integer[]” The postgres extension intarray is needed for this, so you may need to add the line execute “CREATE EXTENSION IF NOT EXISTS intarray” in the correct migration



21
22
23
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/status_for/acts_as_statusable_for.rb', line 21

def initialize_status_for(subject)      
  cattr_accessor :status_for_subject
  if !subject.is_a? Class
    raise "Subject must be defined as a proper class!"
  else
    self.status_for_subject = subject.name
  end      
  
  
  def method_missing(method_id, *subject, &block)
    # This creates a method for self called 'status_for(subject)' that finds all the
    # self items with the 'status_for' with the subject id.
    # Example:  Message.deleted_for(user)
    # Returns array of messages which contains user.id in the message's 'deleted_for'
    if method_id.to_s =~ /^([a-z]+)_for$/
      run_find_status_for($1, subject.first)

    # This creates a method for self called 'not_status_for(subject)' that finds all the
    # self items with the 'status_for' THAT DOES NOT HAVE the subject id.
    # Example:  Message.deleted_for(user)
    # Returns array of messages which DOES NOT contain user.id in the message's 'deleted_for'
    elsif method_id.to_s =~ /^not_([a-z]+)_for$/
      run_find_not_status_for($1, subject.first)
    else
      super
    end 
  end
   
  # Ensuring the method created for the class exists
  def respond_to?(method_id, include_private = false)
    if method_id.to_s =~ /^([a-z]+)_for$/ || method_id.to_s =~ /^not_([a-z]+)_for$/
      true
    else
      super
    end
  end
    
  # The action performed when calling Message.status_for(subject).  Uses a postgres-extension
  # query
  def run_find_status_for(method_id, subject)
    unless subject.class.name == self.status_for_subject
      raise "initialize_status_for is not defined for #{subject.class.name}"
    end
    self.where("idx(#{self.table_name}.#{method_id}_for, #{subject.id})::boolean")
  end

  # The action performed when calling Message.not_status_for(subject).  Uses a postgres-extension
  # query
  def run_find_not_status_for(method_id, subject)
    unless subject.class.name == self.status_for_subject
      raise "initialize_status_for is not defined for #{subject.class.name}"
    end
    self.where("NOT idx(#{self.table_name}.#{method_id}_for, #{subject.id})::boolean") 
  end
  include Status_For_Utils
  include StatusInstanceMethods
end

#respond_to?(method_id, include_private = false) ⇒ Boolean

Ensuring the method created for the class exists

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/status_for/acts_as_statusable_for.rb', line 50

def respond_to?(method_id, include_private = false)
  if method_id.to_s =~ /^([a-z]+)_for$/ || method_id.to_s =~ /^not_([a-z]+)_for$/
    true
  else
    super
  end
end

#run_find_not_status_for(method_id, subject) ⇒ Object

The action performed when calling Message.not_status_for(subject). Uses a postgres-extension query



69
70
71
72
73
74
# File 'lib/status_for/acts_as_statusable_for.rb', line 69

def run_find_not_status_for(method_id, subject)
  unless subject.class.name == self.status_for_subject
    raise "initialize_status_for is not defined for #{subject.class.name}"
  end
  self.where("NOT idx(#{self.table_name}.#{method_id}_for, #{subject.id})::boolean") 
end

#run_find_status_for(method_id, subject) ⇒ Object

The action performed when calling Message.status_for(subject). Uses a postgres-extension query



60
61
62
63
64
65
# File 'lib/status_for/acts_as_statusable_for.rb', line 60

def run_find_status_for(method_id, subject)
  unless subject.class.name == self.status_for_subject
    raise "initialize_status_for is not defined for #{subject.class.name}"
  end
  self.where("idx(#{self.table_name}.#{method_id}_for, #{subject.id})::boolean")
end