Skip to content

objectlateral/rails-concerns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rails::Concerns Code Climate

Common concerns (modules) to be mixed in to Rails models, controllers, and mailers.

Installation

Add this line to your application's Gemfile:

gem "rails-concerns", github: "objectlateral/rails-concerns"

And then execute:

$ bundle

Usage

Just include the modules that your Rails models or controllers are concerned with.

Model Concerns Included

EncodedId

Provides human readable unique strings using each model's id attribute. Great for short URLs. Usage:

class Person < ActiveRecord::Base
  include EncodedId
end

paul = Person.create name: "Paul"
paul.id # => 20355
paul.encoded_id # => "xyz"
Person.where_encoded_id("xyz").first.name => "Paul"

PasswordAuth

Provides basic password setting and authenticating via bcrypt. Including models must have an encrypted_password attribute.

class User < ActiveRecord::Base
  include PasswordAUth
end

user = User.new password: "test1234"
user.authenticate "test1234" # => user object
user.authenticate "test4321" # => false

Slugged

Automatically generates slug fields using array of attributes returned from slug_parts method (defaults to [title]).

class BlogPost < ActiveRecord::Base
  include Slugged

  def slug_parts
    [title, id]
  end
end

post = BlogPost.create title: "Test Post"
post.slug # "test-post"

TokenizedAttributes

Provides easy generation of random SHA tokens for a model's attributes.

class Document < ActiveRecord::Base
  include TokenizedAttributes

  tokenize :slug
end

d = Document.create
d.slug # db89148af5c734ed8f34cb1402b699d63784591f
d.regenerate_token :slug
d.slug # cfa5946b9cb1abc80c02f050f383f06514fb70da

ValidPhone

Provides sanitization, validation, and classify-ication of phone numbers

class Person < ActiveRecord::Base
  include ValidPhone

  valid_phone :cell_phone
end

p = Person.new
p.cell_phone = "867 5309"
p.valid? # false
p.cell_phone = "402 867 5309"
p.valid? # true
p.cell_phone # "4028675309"
p.class_cell_phone # (402) 867-5309

Controller Concerns Included

JsonRenderer

Adds convenience methods for rendering JSON from controllers

class ThingsController < ApplicationController
  include JsonRenderer

  def index
    things = Thing.all
    ok things
  end

  def create
    thing = Thing.create params
    created thing
  end

  def destroy
    thing = Thing.find params[:id]
    thing.destroy
    no_content
  end

  # ... et cetera
end

About

Common Rails concerns used at OL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages