Author: Not specified Language: ruby
Description: Not specified Timestamp: 2017-06-18 10:44:45 +0000
View raw paste Reply
  1. class UserProfile
  2.   attr_reader :user
  3.  
  4.   def initialize(user)
  5.     @user = user
  6.   end
  7.  
  8.   private
  9.  
  10.    def method_missing(method_name)
  11.      string_name = method_name.to_s
  12.      case string_name
  13.      when /^recent_\w+/
  14.        recent_relation(string_name.split('recent_')[1])
  15.      when /\w+_count$/
  16.        relation_count(string_name.split('_count')[0])
  17.      else
  18.        return super unless user.respond_to?(method_name)
  19.        user.send(method_name)
  20.      end
  21.    end
  22.  
  23.    def recent_query
  24.      @recent_query ||= UserRecentQuery.new(user)
  25.    end
  26.  
  27.    def recent_relation(relation_name)
  28.      relation = "#{relation_name}_user".classify.constantize
  29.      recent_query.call(relation.send(:all))
  30.    end
  31.  
  32.    def relation_count(relation_name)
  33.      user.send(relation_name).try(:active).try(:count)
  34.    end
  35. end
View raw paste Reply