Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Sun May 31 05:03:08 -0700 2009 | |
| |
README.md | Thu Sep 24 05:01:29 -0700 2009 | |
| |
Rakefile | Sun May 31 05:03:08 -0700 2009 | |
| |
lib/ | Tue Oct 06 03:23:02 -0700 2009 | |
| |
tasks/ | Sun May 31 05:03:08 -0700 2009 | |
| |
test/ | Sun May 31 05:03:08 -0700 2009 |
TrixyScopes
Collection of useful named scopes for rails apps. Additional methods are based on column name and type ie: Product.name_is("ipod"), Product.price_greater_than(100)
Datetime columns ending with _at get also functional aliases:
Product.updated_at_before('2008-01-01') becomes: Product.updated_before('2008-01-01')
Installation
script/plugin install git://github.com/tomaszmazur/trixy_scopes.git
in your ActiveRecord model:
class Product < ActiveRecord::Base
...
include TrixyScopes
end
to gains access to:
Available named scopes
limit(integer) - limits result to given number of records
Product.limit(5) # => SELECT * FROM `products` LIMIT 5
random - adapter agnostic random
Product.limit(3).random # for SQLite adapter # => SELECT * FROM `sites` ORDER BY RANDOM() LIMIT 3 # for MySql and other adapters: # => SELECT * FROM `sites` ORDER BY RAND() LIMIT 3
latest(integer) - picks up x latest records (ordered by created_at)
Product.latest(5) # => SELECT * FROM `products` ORDER BY `products`.`created_at` desc LIMIT 5
earliest(integer) - picks up x earliest records (ordered by created_at)
Product.earliest(10) # => SELECT * FROM `products` ORDER BY `products`.`created_at` asc LIMIT 10
after(datetime) - picks up records after given datetime
Product.after(1.year.ago) # => SELECT * FROM `products` WHERE (`products`.`created_at` > '2008-06-07 16:11:56')
before(datetime) - picks up records before given datetime
Product.before(Time.now.beginning_of_day) # => SELECT * FROM `products` WHERE (`products`.`created_at` < '2008-06-07 00:00:00')
ALL column types
<attribute_name>_is(attribute_value)
Author.last_name_is("Smith")
# => SELECT * FROM `authors` WHERE (`authors`.`last_name` = 'Smith')
Product.price_is(19.99)
# => SELECT * FROM `products` WHERE (`products`.`price` = 19.99)
<attribute_name>_is_not(attribute_value)
Author.first_name_is_not("John")
# => SELECT * FROM `authors` WHERE (`authors`.`first_name` != 'John')
Product.price_is_not(1_000)
# => SELECT * FROM `products` WHERE (`products`.`price` != 1000)
<attribute_name_plural>_are(array)
Product.ids_are(1,2,3)
# => SELECT * FROM `products` WHERE (`products`.`id` IN (1,2,3))
Author.last_names_are("Smith", "Black")
# => SELECT * FROM `authors` WHERE (`authors`.`last_name` IN ('Smith','Black'))
<attribute_name_plural>_are_not(array)
Product.prices_are_not(0.99, 5.99, 19.99)
# => SELECT * FROM `products` WHERE (`products`.`id` NOT IN (0.99,5.99,19.99))
Author.first_names_are_not("John", "Mike")
# => SELECT * FROM `authors` WHERE (`authors`.`first_name` NOT IN ('John','Mike'))
<attribute_name>_is_nil
User.full_name_is_nil # => SELECT * FROM `users` WHERE (`users`.`full_name` IS NULL)
<attribute_name>_is_not_nil
Product.description_is_not_nil # => SELECT * FROM `products` WHERE (`products`.`description` IS NOT NULL)
STRING columns
<attribute_name>_starts_with(string)
<attribute_name>_ends_with(string)
<attribute_name>_includes(string)
<attribute_name>_matches(regexp)
<attribute_name>_like(string)
<attribute_name>_not_like(string)
BOOLEAN
<attribute_name>
Product.sold # => SELECT * FROM `products` WHERE (`products`.`sold` = 1)
not_<attribute_name>
Product.not_sold # => SELECT * FROM `products` WHERE (`products`.`sold` = 0)
DATETIME
<attribute_name>_before(datetime)
<attribute_name>_after(datetime)
<attribute_name>_between(from, to)
Product.created_between('2009-05-01', '2009-05-31')
# => SELECT * FROM `products` WHERE (`products`.`created_at` BETWEEN '2009-05-01' AND '2009-05-31')
not<attribute_name>between(from, to)
Product.not_created_between('2009-05-01', '2009-05-31')
# => SELECT * FROM `products` WHERE (`products`.`created_at` NOT BETWEEN '2009-05-01' AND '2009-05-31')
INTEGER, FLOAT
<attribute_name>_greater_than(value)
<attribute_name>_greater_or_equal_to(value)
<attribute_name>_less_than(value)
<attribute_name>_less_than_or_equal_to(value)
Copyright
Copyright (c) 2009 Tomasz Mazur, released under the MIT license







