All Projects → mirego → microscope

mirego / microscope

Licence: BSD-3-Clause license
🔬 Microscope adds useful scopes targeting ActiveRecord boolean, date and datetime fields.

Programming Languages

ruby
36898 projects - #4 most used programming language
Roff
2310 projects

Projects that are alternatives of or similar to microscope

active dynamic
Gem that allows to add attributes to your active models dynamically
Stars: ✭ 61 (+17.31%)
Mutual labels:  activerecord
activerecord-mysql2rgeo-adapter
ActiveRecord connection adapter for gis, based on mysql and rgeo
Stars: ✭ 25 (-51.92%)
Mutual labels:  activerecord
sinatras-skeleton
Basic Sinatra Skeleton MVC CRUD App with Sprockets, Warden, ActiveRecord and PostgresQL
Stars: ✭ 13 (-75%)
Mutual labels:  activerecord
activerecord-crate-adapter
Ruby on Rails ActiveRecord adapter for CrateDB
Stars: ✭ 27 (-48.08%)
Mutual labels:  activerecord
duck record
Used for creating virtual models like ActiveType or ModelAttribute does.
Stars: ✭ 23 (-55.77%)
Mutual labels:  activerecord
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+57.69%)
Mutual labels:  activerecord
blade-jdbc
🐜 move to https://github.com/biezhi/anima
Stars: ✭ 36 (-30.77%)
Mutual labels:  activerecord
nxt state machine
A simple but powerful state machine implementation.
Stars: ✭ 14 (-73.08%)
Mutual labels:  activerecord
database cleaner-active record
Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing.
Stars: ✭ 35 (-32.69%)
Mutual labels:  activerecord
Neo
Orm框架:基于ActiveRecord思想开发的至简化的java的Orm框架
Stars: ✭ 35 (-32.69%)
Mutual labels:  activerecord
no querying views
No more querying views in your Rails apps
Stars: ✭ 60 (+15.38%)
Mutual labels:  activerecord
ebisu connection
EbisuConnection allows access to replica servers
Stars: ✭ 14 (-73.08%)
Mutual labels:  activerecord
active record-updated at
Touch `updated_at` by default with calls to `update_all` and `update_column(s)`
Stars: ✭ 27 (-48.08%)
Mutual labels:  activerecord
departure
Percona's pt-online-schema-change runner for ActiveRecord migrations.
Stars: ✭ 86 (+65.38%)
Mutual labels:  activerecord
paper trail-association tracking
Plugin for the PaperTrail gem to track and reify associations
Stars: ✭ 103 (+98.08%)
Mutual labels:  activerecord
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (-40.38%)
Mutual labels:  activerecord
alloy
Patchwork now rename to [alloy], alloy is a simple toolkit that makes your iOS / OS X apps development more easier.
Stars: ✭ 61 (+17.31%)
Mutual labels:  activerecord
index shotgun
duplicate index checker 🔥 🔫 👮
Stars: ✭ 35 (-32.69%)
Mutual labels:  activerecord
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-59.62%)
Mutual labels:  activerecord
prefixed ids
Friendly Prefixed IDs for your Ruby on Rails models
Stars: ✭ 159 (+205.77%)
Mutual labels:  activerecord
Microscope


Microscope adds useful scopes targeting ActiveRecord boolean, date and datetime attributes.


Installation

Add this line to your application's Gemfile:

gem 'microscope'

Usage

create_table "events" do |t|
  t.string   "name"
  t.boolean  "special"
  t.datetime "expired_at"
  t.date     "archived_on"
end

class Event < ApplicationRecord
  acts_as_microscope
end

Event.special
# SELECT * FROM `events` where `events`.`special` = 1

Event.not_special
# SELECT * FROM `events` where `events`.`special` = 0

Time.now
# => 2013-07-05 15:43:42

Event.expired_before(2.months.ago)
# SELECT * FROM `events` where `events`.`expired_at` < '2013-05-05 15:43:42'

Event.expired_before_now
# SELECT * FROM `events` where `events`.`expired_at` < '2013-07-05 15:43:42'

Event.expired_after_or_now
# SELECT * FROM `events` where `events`.`expired_at` >= '2013-07-05 15:43:42'

Event.expired_after_or_at(2.months.from_now)
# SELECT * FROM `events` where `events`.`expired_at` >= '2013-09-05 15:43:42'

Event.expired_between(2.months.ago..1.month.from_now)
# SELECT * FROM `events` where `events`.`expired_at` BETWEEN '2013-05-05 15:43:42' AND '2013-08-05 15:43:42'

Event.archived_before(2.days.ago)
# SELECT * FROM `events` where `events`.`archived_on` < '2013-07-03'

Event.archived_between(2.days.ago..3.days.from_now)
# SELECT * FROM `events` where `events`.`archived_on` BETWEEN '2013-07-03' AND '2013-07-08'

Event.archived
# SELECT * FROM `events` where `events`.`archived_on` IS NOT NULL AND `events`.`archived_on` <= '2013-07-05 15:43:42'

Event.not_archived
# SELECT * FROM `events` where `events`.`archived_on` IS NULL OR `events`.`archived_on` > '2013-07-05 15:43:42'

Microscope also adds a few instance methods to the model per scope.

event = Event.archived.first
# SELECT * FROM `events` where `events`.`archived_on` IS NOT NULL AND `events`.`archived_on` <= '2013-07-05 15:43:42' LIMIT 1

event.archived? # => true
event.not_archived? # => false

event = Event.unarchived.first
event.archived? # => false

event.mark_as_archived
event.archived? # => true
event.archived_at # => 2013-07-05 15:43:42
event.reload
event.archived? # => false
event.archived_at # => nil

event.mark_as_archived! # (same as #mark_as_archived but save the record immediately)
event.archived? # => true
event.archived_at # => 2013-07-05 15:43:42
event.reload
event.archived? # => true
event.archived_at # => 2013-07-05 15:43:42

Options

On acts_as_microscope

You can also use a few options when calling acts_as_microscope:

class Event < ApplicationRecord
  acts_as_microscope only: [:created_at]
end

class User < ApplicationRecord
  acts_as_microscope except: [:created_at]
end

Event.created_before(2.months.ago) # works!
Event.updated_before(2.months.ago) # NoMethodError

User.created_before(2.months.ago) # NoMethodError
User.updated_before(2.months.ago) # works!

Date and time attributes and forms

When using Microscope on a date or datetime attribute, you’ll probably want avoid dealing with those values in forms and only expose a checkbox to the user.

Setting a Microscope-enabled attribute to a truthy value will correctly cast the attribute to the right value (Date.today or Time.now, respectively). Otherwise the attribute will be set to nil.

Model

class Event < ApplicationRecord
  acts_as_microscope
end

View

<% form_for @event do |form| %>
  <%= form.text_field :name %>
  <%= form.check_box :archived? %>

  <%= submit_tag %>
<% end %>

Controller

class EventsController < ApplicationController
  def new
    @event = Event.new
  end

  def create
    @event = Event.new(event_params)
    @event.save
  end

  def event_params
    params.require(:event).permit(:name, :archived)
  end
end

License

Microscope is © 2013-2019 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

The microscope logo is based on this lovely icon by Scott Lewis, from The Noun Project. Used under a Creative Commons BY 3.0 license.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].