Sha256: 23e200e40971a3d95724af95345663e5965f842ee1538f7c0f77e0155d579362

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module MayNeedReview
  
  def may_need_review
    
    raise 'The model needs to be able to have flags associated.' if not self.instance_methods.include? :flags
    
    extend ClassMethods
    include InstanceMethods
    
  end
  
  module ClassMethods
  
    # This class method gets all ProfileFields that are marked as :needs_review.
    # 
    def find_all_that_need_review
      self.joins(:flags).where('flags.key' => :needs_review, 'flags.flagable_type' => 'ProfileField')
    end
    
    # This class method gets all ProfileFields that match the given ids
    # and are marked as :needs_review.
    #
    def find_all_that_need_review_by_profile_field_ids(profile_field_ids)
      self.find_all_that_need_review.where(id: profile_field_ids)
    end
    
  end

  module InstanceMethods
    
    def needs_review?
      has_flag? :needs_review
    end
    def needs_review!
      self.needs_review = true
    end
    def needs_review
      self.needs_review?
    end
    def needs_review=(new_needs_review)
      new_needs_review = false if new_needs_review == "false"
      if new_needs_review != self.needs_review
        attribute_will_change!(:needs_review) 
      end
      if new_needs_review
        self.add_flag :needs_review
      else
        self.remove_flag :needs_review
      end
    end
    
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
your_platform-1.0.1 app/models/may_need_review.rb
your_platform-1.0.0 app/models/may_need_review.rb
your_platform-0.0.2 app/models/may_need_review.rb