adsteel / composable_decorator Goto Github PK
View Code? Open in Web Editor NEWDEPRECATED - A simple, composable decorator for Rails models
License: MIT License
DEPRECATED - A simple, composable decorator for Rails models
License: MIT License
module AuthorDecorator
def decorated_method
"author decorated method"
end
end
module StoreDecorator
def decorated_method
"store decorated method"
end
end
class Store
has_many :stetsons
decorate_with StoreDecorator
end
class Author
has_many :hats
decorate_with AuthorDecorator
end
class Hat
belongs_to :author
delegate_decorated_to :author
end
class Stetson < Hat
belongs_to :store
delegate_decorated_to :store
end
stetson = Stetson.new.decorate
stetson.author_decorated_method
stetson.store_decorated_method
# define decorators based on role
decorate_with UserDecorator #default
decorate_with UserDecorator, AdminDecorator, as: :admin
# decorate based on role
user.decorate #uses UserDecorator
user.decorate(:admin) #uses UserDecorator and AdminDecorator
Simply include the class and instance methods in ActiveRecord, and save include ComposableDecorator
for the decorators, to provide additional functionality.
e.g.
module UserDecorator
include ComposableDecorator
# example of additional functionality
handle_nil :first_name, :last_name, with: '-'
end
Currently, delegations classify the name, rather than get the actual class of the association.
e.g.
# currently, this will bomb
delegate_decorated_to :most_recent_user # tries to find class MostRecentUser, rather than User
Allow decorators to easily handle nil values with a default string.
module UserDecorator
handle_nil :name, :email, with: '-'
end
user = User.create(name: nil, email: nil, phone: nil)
user.name #=> '-'
user.email #=> '-'
user.phone #=> nil
class User
decorate_with UserDecorator
end
class Admin < User
# this decoration should use both the UserDecorator and the AdminDecorator
# currently the parent decorator is clobbered
decorate_with AdminDecorator
end
module UserDecorator
declare_time :created_at, with: MyTimeClass
end
class MyTimeClass < TimeFormatBase
def my_time
@string.strftime("%I:%M%p")
end
def my_date
@string.striftime("%m/%d/%y")
end
end
user.decorate
# returns db value string as default, but as a MyTimeClass object.
user.created_at
# acts like String, can also call methods defined in declared class
user.created_at.my_date # returns date format
user.created_at.my_time # returns time format
# also creates delegated, prefixed methods
user.created_at_my_date # returns date format
user.created_at_my_time # returns time format
Currently the first method call in this example will fail.
class Hat
delegate_decorated_to :owner
delegate_decorated_to :company, prefix: false
end
hat.decorate.owner_decorator_method #=> 'Owner decorator method!'
hat.decorate.decorator_method #=> 'Company decorator method!'
Currently record.decorate
calls all association records and decorates them. This can result in a tremendous number of unneeded db calls. These associations should be decorated only when called on a decorated object.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.