Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2304

Merge of default_scope instead of overwrite

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.5
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Today I've encountered a certain problem with default_scope.

      If you for example define a class

      class Post
        include Mongoid::Document
        include Mongoid::Timestamps
      
        field :title, type: String
        field :text, type: String
      
        default_scope desc(:title).desc(:created_at)
      end
      

      Maybe you want to apply

      Unable to find source-code formatter for language: desc. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      (:created_at)

      to more entities you have. Why not to create a module that provides that functionality?

      module FancyTimestamps
        extend ActiveSupport::Concern
      
        self.included do
          include Mongoid::Timestamps      
      
          default_scope desc(:created_at)
        end
      end
      

      Then you can

      Unable to find source-code formatter for language: include. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       FancyTimestamps

      into your entities, but then

      Unable to find source-code formatter for language: default_scope```. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       (if you provide one inside the entity) will overwrite 

      default_scope

       defined inside 

      FancyTimestamps{{`}}.

      That's the main reason behind this patch.

            Assignee:
            Unassigned Unassigned
            Reporter:
            netoneko netoneko
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: