Giter Site home page Giter Site logo

composable_decorator's People

Contributors

adsteel avatar

Stargazers

 avatar  avatar

composable_decorator's Issues

Todo: Inherit delegate_decorated_to

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

Todo: Allow decorators by role

# 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

Todo: Allow decorators to get methods from ComposableDecorator

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

Todo: Base delgation on association class, not name

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

Todo: handle_nil macro

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

Todo: Allow decorators to inherit

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

#declare_date macro in decorator

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

Minimize db calls when decorating

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.