time_scopes 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +25 -0
- data/Rakefile +52 -0
- data/lib/time_scopes/time_scopes.rb +109 -0
- data/lib/time_scopes.rb +3 -0
- metadata +86 -0
    
        data/README.rdoc
    ADDED
    
    | @@ -0,0 +1,25 @@ | |
| 1 | 
            +
            == TrixyScopes
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Useful scopes for ActiveRecord based on Date/DateTime columns
         | 
| 4 | 
            +
             | 
| 5 | 
            +
             | 
| 6 | 
            +
            == Installation
         | 
| 7 | 
            +
             | 
| 8 | 
            +
             | 
| 9 | 
            +
              gem install time_scopes
         | 
| 10 | 
            +
             | 
| 11 | 
            +
             | 
| 12 | 
            +
            include TimeScopes module into your ActiveRecord model:
         | 
| 13 | 
            +
             | 
| 14 | 
            +
             | 
| 15 | 
            +
              class Product < ActiveRecord::Base
         | 
| 16 | 
            +
              
         | 
| 17 | 
            +
                include TimeScopes
         | 
| 18 | 
            +
                
         | 
| 19 | 
            +
                (...)
         | 
| 20 | 
            +
                
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            == Copyright
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            Copyright (c) 2010 Tomasz Mazur, released under the MIT license
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,52 @@ | |
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'rake/gempackagetask'
         | 
| 3 | 
            +
            require 'rake/testtask'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module TimeScopes
         | 
| 6 | 
            +
              module Version
         | 
| 7 | 
            +
                
         | 
| 8 | 
            +
                MAJOR = 0
         | 
| 9 | 
            +
                MINOR = 0
         | 
| 10 | 
            +
                TINY  = 1
         | 
| 11 | 
            +
                
         | 
| 12 | 
            +
                def self.to_s # :nodoc:
         | 
| 13 | 
            +
                  [MAJOR, MINOR, TINY].join('.')
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
                
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
            end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            spec = Gem::Specification.new do |s|
         | 
| 20 | 
            +
              s.name             = 'time_scopes'
         | 
| 21 | 
            +
              s.version          = TimeScopes::Version.to_s
         | 
| 22 | 
            +
              s.has_rdoc         = false
         | 
| 23 | 
            +
              s.extra_rdoc_files = %w(README.rdoc)
         | 
| 24 | 
            +
              s.rdoc_options     = %w(--main README.rdoc)
         | 
| 25 | 
            +
              s.summary          = "scopes for ActiveRecord 3"
         | 
| 26 | 
            +
              s.author           = 'Tomasz Mazur'
         | 
| 27 | 
            +
              s.email            = 'defkode@gmail.com'
         | 
| 28 | 
            +
              s.homepage         = 'http://trix.pl'
         | 
| 29 | 
            +
              s.files            = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
         | 
| 30 | 
            +
              # s.executables    = ['trixy_scopes']
         | 
| 31 | 
            +
              
         | 
| 32 | 
            +
              s.add_dependency('activerecord', '~> 3.0.0')
         | 
| 33 | 
            +
            end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            Rake::GemPackageTask.new(spec) do |pkg|
         | 
| 36 | 
            +
              pkg.gem_spec = spec
         | 
| 37 | 
            +
            end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            Rake::TestTask.new do |t|
         | 
| 40 | 
            +
              t.libs << 'test'
         | 
| 41 | 
            +
              t.test_files = FileList["test/**/*_test.rb"]
         | 
| 42 | 
            +
              t.verbose = true
         | 
| 43 | 
            +
            end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            desc 'Generate the gemspec for the Gem (useful when serving from Github)'
         | 
| 46 | 
            +
            task :gemspec do
         | 
| 47 | 
            +
              file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
         | 
| 48 | 
            +
              File.open(file, 'w') {|f| f << spec.to_ruby }
         | 
| 49 | 
            +
              puts "Created gemspec: #{file}"
         | 
| 50 | 
            +
            end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            task :github => :gemspec
         | 
| @@ -0,0 +1,109 @@ | |
| 1 | 
            +
            module TrixyScopes 
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              def self.included(klass)
         | 
| 4 | 
            +
                klass.class_eval do
         | 
| 5 | 
            +
                  return unless klass.table_exists?
         | 
| 6 | 
            +
                  
         | 
| 7 | 
            +
                  connection = ActiveRecord::Base.connection
         | 
| 8 | 
            +
                  adapter    = connection.adapter_name
         | 
| 9 | 
            +
                  
         | 
| 10 | 
            +
                  table_name = klass.table_name
         | 
| 11 | 
            +
                  quoted_table_name = klass.quoted_table_name
         | 
| 12 | 
            +
                  
         | 
| 13 | 
            +
                  named_scope :random, :order => adapter == "SQLite" ? "RANDOM()" : "RAND()"
         | 
| 14 | 
            +
                  named_scope :order_by, lambda {|sort_by, sort_order| { :order => "#{sort_by} #{sort_order}" } }
         | 
| 15 | 
            +
                  named_scope :inner_join, lambda { |reflection| { :joins => reflection } }
         | 
| 16 | 
            +
                  named_scope :left_join, lambda { |reflection|
         | 
| 17 | 
            +
                    r = klass.reflections[reflection]
         | 
| 18 | 
            +
                    reflection_table_name = r.class_name.constantize.quoted_table_name
         | 
| 19 | 
            +
                    reflection_primary_key = r.class_name.constantize.primary_key
         | 
| 20 | 
            +
                    
         | 
| 21 | 
            +
                    {
         | 
| 22 | 
            +
                     :joins => "LEFT JOIN #{reflection_table_name} ON #{reflection_table_name}.#{reflection_primary_key} = #{table_name}.#{r.association_foreign_key}"
         | 
| 23 | 
            +
                    }
         | 
| 24 | 
            +
                  }
         | 
| 25 | 
            +
                  named_scope :limit, lambda { |limit| { :limit => limit } }
         | 
| 26 | 
            +
                  named_scope :latest, lambda { |limit| { :limit => limit, :order => "#{quoted_table_name}.`created_at` DESC" } } 
         | 
| 27 | 
            +
                  named_scope :earliest, lambda { |limit| { :limit => limit, :order => "#{quoted_table_name}.`created_at` ASC" } }
         | 
| 28 | 
            +
                  
         | 
| 29 | 
            +
                  klass.columns.each do |column|
         | 
| 30 | 
            +
                    column_name = column.name
         | 
| 31 | 
            +
                    
         | 
| 32 | 
            +
                    quoted_column_name = [quoted_table_name, connection.quote_column_name(column_name)].join(".")
         | 
| 33 | 
            +
                    
         | 
| 34 | 
            +
                    named_scope "#{column_name}_is", lambda { |value| { :conditions => { column_name => value } } }
         | 
| 35 | 
            +
                    named_scope "#{column_name}_is_not", lambda { |value| { :conditions => ["#{quoted_column_name} != ?", value] } }
         | 
| 36 | 
            +
                    named_scope "#{column_name}_is_nil", :conditions => { column_name => nil }
         | 
| 37 | 
            +
                    named_scope "#{column_name}_is_not_nil", :conditions => "#{quoted_column_name} IS NOT NULL"
         | 
| 38 | 
            +
                    
         | 
| 39 | 
            +
                    named_scope "#{column_name.pluralize}_are", lambda { |*ids| { :conditions => { column_name => ids.flatten } } }
         | 
| 40 | 
            +
                    named_scope "#{column_name.pluralize}_are_not", lambda { |*ids| { :conditions => ["#{quoted_column_name} NOT IN(?)", ids.flatten] } }
         | 
| 41 | 
            +
                    
         | 
| 42 | 
            +
                    unless column.type == :boolean
         | 
| 43 | 
            +
                      named_scope "#{column_name}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
         | 
| 44 | 
            +
                      named_scope "#{column_name}_not_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
         | 
| 45 | 
            +
                    end
         | 
| 46 | 
            +
                    
         | 
| 47 | 
            +
                    unless column.type == :boolean || column.type == :text
         | 
| 48 | 
            +
                      named_scope "#{column_name}_in", lambda { |*elements| { :conditions => { column_name => elements.flatten } } }
         | 
| 49 | 
            +
                      named_scope "#{column_name}_not_in", lambda { |*elements| { :conditions => ["#{quoted_column_name} NOT IN(?)", elements.flatten] } }
         | 
| 50 | 
            +
                    end
         | 
| 51 | 
            +
                    
         | 
| 52 | 
            +
                    if column.name == "expires_at"
         | 
| 53 | 
            +
                      named_scope "expired", lambda { { :conditions => ["#{quoted_column_name} < ?", Time.zone.now] } }
         | 
| 54 | 
            +
                      named_scope "valid", lambda { { :conditions => ["#{quoted_column_name} >= ?", Time.zone.now] } }
         | 
| 55 | 
            +
                    end
         | 
| 56 | 
            +
                            
         | 
| 57 | 
            +
                    if column.type == :datetime
         | 
| 58 | 
            +
                      named_scope "#{column_name}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
         | 
| 59 | 
            +
                      named_scope "#{column_name}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
         | 
| 60 | 
            +
                      
         | 
| 61 | 
            +
                      if column_name.last(3) == "_at"
         | 
| 62 | 
            +
                        column_name_alias = column_name.chomp("_at")
         | 
| 63 | 
            +
                        named_scope "#{column_name_alias}_today", lambda { { :conditions => ["#{quoted_column_name} > ?", Time.zone.now.beginning_of_day] } }
         | 
| 64 | 
            +
                        named_scope "#{column_name_alias}_yesterday", lambda { { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", Time.zone.now.yesterday.beginning_of_day, Time.zone.now.yesterday.end_of_day] } }
         | 
| 65 | 
            +
                        
         | 
| 66 | 
            +
                        named_scope "#{column_name_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
         | 
| 67 | 
            +
                        named_scope "not_#{column_name_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
         | 
| 68 | 
            +
                        named_scope "#{column_name_alias}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
         | 
| 69 | 
            +
                        named_scope "#{column_name_alias}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
         | 
| 70 | 
            +
                        
         | 
| 71 | 
            +
                        named_scope "#{column_name_alias}_in_last_week",  lambda { { :conditions => ["#{quoted_column_name} > ?", 1.week.ago] } }
         | 
| 72 | 
            +
                        named_scope "#{column_name_alias}_in_last_month", lambda { { :conditions => ["#{quoted_column_name} > ?", 1.month.ago] } }
         | 
| 73 | 
            +
                        named_scope "#{column_name_alias}_in_last_year",  lambda { { :conditions => ["#{quoted_column_name} > ?", 1.year.ago] } }
         | 
| 74 | 
            +
                        named_scope "#{column_name_alias}_in_month",      lambda { |datetime| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", datetime.beginning_of_month, datetime.end_of_month] } }
         | 
| 75 | 
            +
                        named_scope "#{column_name_alias}_in_day",        lambda { |datetime| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", datetime.beginning_of_day, datetime.end_of_day] } }
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                        named_scope "#{column_name_alias}_ago", lambda { |period_type, period_count| { :conditions => ["#{quoted_column_name} > ?", period_count.send(period_type).ago] } }
         | 
| 78 | 
            +
                      end
         | 
| 79 | 
            +
                    end
         | 
| 80 | 
            +
                    
         | 
| 81 | 
            +
                    if column.type == :integer || column.type == :float
         | 
| 82 | 
            +
                      named_scope "#{column_name}_eql", lambda { |value| { :conditions => { column_name => value } } }
         | 
| 83 | 
            +
                      named_scope "#{column_name}_gt", lambda { |value| { :conditions => ["#{quoted_column_name} > ?", value] } }
         | 
| 84 | 
            +
                      named_scope "#{column_name}_gte", lambda { |value| { :conditions => ["#{quoted_column_name} >= ?", value] } }
         | 
| 85 | 
            +
                      named_scope "#{column_name}_lt", lambda { |value| { :conditions => ["#{quoted_column_name} < ?", value] } }
         | 
| 86 | 
            +
                      named_scope "#{column_name}_lte", lambda { |value| { :conditions => ["#{quoted_column_name} <= ?", value] } }
         | 
| 87 | 
            +
                    end
         | 
| 88 | 
            +
                    
         | 
| 89 | 
            +
                    if column.type == :string
         | 
| 90 | 
            +
                      named_scope "#{column_name}_starts_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "#{string}%"] } }
         | 
| 91 | 
            +
                      named_scope "#{column_name}_ends_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}"] } }
         | 
| 92 | 
            +
                      named_scope "#{column_name}_includes", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}%"] } }
         | 
| 93 | 
            +
                    end
         | 
| 94 | 
            +
                    
         | 
| 95 | 
            +
                    if column.type == :text || column.type == :string
         | 
| 96 | 
            +
                      named_scope "#{column_name}_like", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", string] } }
         | 
| 97 | 
            +
                      named_scope "#{column_name}_not_like", lambda { |string| { :conditions => ["#{quoted_column_name} NOT LIKE ?", string] } }
         | 
| 98 | 
            +
                    end
         | 
| 99 | 
            +
                    
         | 
| 100 | 
            +
                    if column.type == :boolean
         | 
| 101 | 
            +
                      named_scope "#{column_name}", :conditions => { column_name => true }
         | 
| 102 | 
            +
                      named_scope "not_#{column_name}", :conditions => { column_name => false }
         | 
| 103 | 
            +
                    end
         | 
| 104 | 
            +
                  end
         | 
| 105 | 
            +
                  
         | 
| 106 | 
            +
                end
         | 
| 107 | 
            +
              end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
            end
         | 
    
        data/lib/time_scopes.rb
    ADDED
    
    
    
        metadata
    ADDED
    
    | @@ -0,0 +1,86 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            name: time_scopes
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              hash: 29
         | 
| 5 | 
            +
              prerelease: false
         | 
| 6 | 
            +
              segments: 
         | 
| 7 | 
            +
              - 0
         | 
| 8 | 
            +
              - 0
         | 
| 9 | 
            +
              - 1
         | 
| 10 | 
            +
              version: 0.0.1
         | 
| 11 | 
            +
            platform: ruby
         | 
| 12 | 
            +
            authors: 
         | 
| 13 | 
            +
            - Tomasz Mazur
         | 
| 14 | 
            +
            autorequire: 
         | 
| 15 | 
            +
            bindir: bin
         | 
| 16 | 
            +
            cert_chain: []
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            date: 2010-07-01 00:00:00 +02:00
         | 
| 19 | 
            +
            default_executable: 
         | 
| 20 | 
            +
            dependencies: 
         | 
| 21 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 22 | 
            +
              name: activerecord
         | 
| 23 | 
            +
              prerelease: false
         | 
| 24 | 
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 25 | 
            +
                none: false
         | 
| 26 | 
            +
                requirements: 
         | 
| 27 | 
            +
                - - ~>
         | 
| 28 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 29 | 
            +
                    hash: 7
         | 
| 30 | 
            +
                    segments: 
         | 
| 31 | 
            +
                    - 3
         | 
| 32 | 
            +
                    - 0
         | 
| 33 | 
            +
                    - 0
         | 
| 34 | 
            +
                    version: 3.0.0
         | 
| 35 | 
            +
              type: :runtime
         | 
| 36 | 
            +
              version_requirements: *id001
         | 
| 37 | 
            +
            description: 
         | 
| 38 | 
            +
            email: defkode@gmail.com
         | 
| 39 | 
            +
            executables: []
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            extensions: []
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            extra_rdoc_files: 
         | 
| 44 | 
            +
            - README.rdoc
         | 
| 45 | 
            +
            files: 
         | 
| 46 | 
            +
            - README.rdoc
         | 
| 47 | 
            +
            - Rakefile
         | 
| 48 | 
            +
            - lib/time_scopes/time_scopes.rb
         | 
| 49 | 
            +
            - lib/time_scopes.rb
         | 
| 50 | 
            +
            has_rdoc: true
         | 
| 51 | 
            +
            homepage: http://trix.pl
         | 
| 52 | 
            +
            licenses: []
         | 
| 53 | 
            +
             | 
| 54 | 
            +
            post_install_message: 
         | 
| 55 | 
            +
            rdoc_options: 
         | 
| 56 | 
            +
            - --main
         | 
| 57 | 
            +
            - README.rdoc
         | 
| 58 | 
            +
            require_paths: 
         | 
| 59 | 
            +
            - lib
         | 
| 60 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 61 | 
            +
              none: false
         | 
| 62 | 
            +
              requirements: 
         | 
| 63 | 
            +
              - - ">="
         | 
| 64 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 65 | 
            +
                  hash: 3
         | 
| 66 | 
            +
                  segments: 
         | 
| 67 | 
            +
                  - 0
         | 
| 68 | 
            +
                  version: "0"
         | 
| 69 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 70 | 
            +
              none: false
         | 
| 71 | 
            +
              requirements: 
         | 
| 72 | 
            +
              - - ">="
         | 
| 73 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 74 | 
            +
                  hash: 3
         | 
| 75 | 
            +
                  segments: 
         | 
| 76 | 
            +
                  - 0
         | 
| 77 | 
            +
                  version: "0"
         | 
| 78 | 
            +
            requirements: []
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            rubyforge_project: 
         | 
| 81 | 
            +
            rubygems_version: 1.3.7
         | 
| 82 | 
            +
            signing_key: 
         | 
| 83 | 
            +
            specification_version: 3
         | 
| 84 | 
            +
            summary: scopes for ActiveRecord 3
         | 
| 85 | 
            +
            test_files: []
         | 
| 86 | 
            +
             |