Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/linki/getter_for
Adds getter methods for attributes that the object belongs to
https://github.com/linki/getter_for
Last synced: about 2 months ago
JSON representation
Adds getter methods for attributes that the object belongs to
- Host: GitHub
- URL: https://github.com/linki/getter_for
- Owner: linki
- License: mit
- Created: 2009-12-11T11:27:38.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2009-12-11T15:40:15.000Z (about 15 years ago)
- Last Synced: 2024-10-12T18:53:36.940Z (3 months ago)
- Language: Ruby
- Homepage:
- Size: 82 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.textile
- License: LICENSE
Awesome Lists containing this project
README
h1. Getter For
gem install getter_forh2. Setup
image you have a ticket model that belongs to a user. you can add convenience methods for any user attribute like that:
class Ticket < ActiveRecord::Base
belongs_to :user
getter_for :user => :name
end
@ticket = Ticket.new
@ticket.user_name # => @ticket.user.name if @ticket.userThat method returns nil if user is nil, so it can safely used in your views
You can pass multiple key/value pairs separated by comma and provide arrays for both model name and method name. let me show you:
class Project < ActiveRecord::Base
belongs_to :category
belongs_to :user
belongs_to :assignee, :class_name => 'User'
getter_for [:category, :user] => :name,
:assignee => [:email, :phone]
end
@project = Project.new
@project.category_name # => @project.category.name if @project.category
@project.user_name # => @project.user.name if @project.user
@project.assignee_email # => @project.assignee.email if @project.assignee
@project.assignee_phone # => @project.assignee.phone if @project.assigneeyou can also make fancy stuff like:
class User < ActiveRecord::Base
belongs_to :department
getter_for :department => :name # => user.department_name
end
class Comment < ActiveRecord::Base
belongs_to :user
getter_for :user => :department_name # => comment.user_department_name
end
@comment = Comment.new
@comment.user_department_name # => @comment.user.department.name if @comment.user && @comment.user.departmentCopyright (c) 2009 Martin Linkhorst, released under the MIT license.