1. Ruby / Говнокод #7645

    −104

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    def entities(model, params = {})
            @entities                 ||= {}
            @entities[model]    ||= []
    
            if @entities[model].blank? or parameters_changed?(model, params)
              @entities[model]  = []
              include           = params[:include]  || nil
              group             = params[:group]    || nil
              order             = params[:order]    || nil
              page              = params[:page]     || nil
              entity_ids        = []
              model_role        = nil
              model_class       = model.to_s.classify.constantize
    
              if self.has_role_for?(model_class)
                self.roles_for(model_class).uniq.each do |role|
                  if role.authorizable_id.blank?
                    raise "Authorization problem! Found more than one #{model_class} model permission!" unless model_role.blank?
                    model_role = role.name
                    next
                  end
    
                  if !role.authorizable_id.blank? && MerchantRole::MERCHANT_ROLES.include?(role.name)
                    entity_ids << role.authorizable.id
                  end
                end
    
                if entity_ids.blank? and MerchantRole::MERCHANT_ROLES.include?(model_role)
                  #WARNING! RECURSION! EVIL!
                  if MerchantRole.has_entity_parent?(model)
                    parent_model    = MerchantRole.entity_parent(model)
                    parent_entities = self.entities(parent_model)
                    unless parent_entities.blank?
                      _params = {
                        :conditions => conditions_and_parameters(params, nil, ["`#{model.to_s}`.`#{parent_model.to_s.singularize}_id` IN (?)", parent_entities.map(&:id)]),
                        :include => include,
                        :order => order,
                        :group => group
                      }
                      _params.merge!(:page => page) if params.keys.include?(:page)
                      if block_given?
                        (self.is_reseller? ? self.reseller.send(model) : model_class).each(_params) do |o|
                          yield o
                        end
                      else
                        @entities[model] = (self.is_reseller? ? self.reseller.send(model) : model_class).send(params.keys.include?(:page) ? :paginate : :find, :all, _params)
                      end
                    end
                  else
                    _params = {
                      :conditions => conditions_and_parameters(params, nil),
                      :include => include,
                      :order => order,
                      :group => group
                    }
                    _params.merge!(:page => page) if params.keys.include?(:page)
       
                    if block_given?    #Find only merchants associated with the reseller or all merchants if we are no reseller.
                      (self.is_reseller? ? self.reseller.send(model) : model_class).each(_params) do |o|
                        yield o
                      end
                    else
                      @entities[model] = (self.is_reseller? ? self.reseller.send(model) : model_class).send(params.keys.include?(:page) ? :paginate : :find, :all, _params)
                    end
                  end
                else
                  _params = { :conditions => conditions_and_parameters(params, nil, ["`#{model.to_s}`.`id` IN (?)", entity_ids]),
                                       :include => include,
                                       :order => order,
                                       :group => group }
                   _params.merge!(:page => page) if params.keys.include?(:page)
                   if block_given?
                     model_class.each(_params) do |o|
                       yield o
                     end
                   else
                     @entities[model] = model_class.send(params.keys.include?(:page) ? :paginate : :find, :all, _params)
                   end
                 end
               end
            end
         entities[@model]
       end

    This method does the following (you guessed it, right?):

    #Fetch entities for which we have a read permission.
    #This is now it should work:
    #1. : If we don't have any role on the model, we won't get anything.
    #2a.: Check roles we have for whole model. If no role for whole model we can only fetch instances we have assigned.
    #2b.: Check all instances we have a role for and store them.
    #3a.: We have roles for instances -> Goto(#7)
    #3b.: We have no instance roles but have a role for the whole model.
    #4. : Check recusively if we have roles fro a parent model (channels -> merchants).
    #5a.: If we have a parent role and got parent entities returned we only fetch instances belonging to the parent entities.
    #6a.: If we have a parent role and got no entities back, then we won't get anything since we are not supposed to.
    #7. : Get all instances based on our reseller or globally for the system.

    Запостил: emilpetkov, 25 Августа 2011

    Комментарии (6) RSS

    • Руби. Мясо. Руби - это глагол.
      Ответить
    • include = params[:include] || nil
      чет не понял глубокий смысл этого...
      если params[:include] nil то тогда это равноценно
      include = nil || nil

      вообще дальше много букв не осилил... я ж надеюсь что тебе там нечего пофиксить не надо!? :)
      Ответить
    • Попробовал код. Что-то странное с моей командной строкой…
      Ответить
    • #WARNING! RECURSION! EVIL!
      # GOTO CONSIDERED HARMFULL!
      # IT MAY FAIL!
      # "MEAT".chop!
      Ответить

    Добавить комментарий