Giter VIP home page Giter VIP logo

dekorator's Issues

Avoid deep decoration

decorate(decorate(Post.new)) should not decorate the decorated object.

def decorate(object_or_collection, with: nil)
  return object_or_collection if object_or_collection.blank? || object_or_collection.is_a?(Dekorator::Base) || object_or_collection.is_a?(DecoratedEnumerableProxy)

  ...
end

Improve Railtie

We should update the Railtie to update the autoload path as following.

config.after_initialize do |app|
  app.config.paths.add 'app/decorators', eager_load: true
end

Concern to have #decorated on model

Does a concern could be a nice to have for model to access decorated object by itself.

Something like

module Decorable
  def decorated
    @decorate ||= decorate_class.decorate(self)
  end

  def decorator_class
    @decorator_class ||= "#{object_or_enumerable.class}Decorator".safe_constantize
  end
end

So in a model

class User < ApplicationRecord
  include Decorable
  ...

  def decorator_class
    CustomDecorator
  end
end

user = User.first # => User
user.decorated # => CustomDecorator

Test generators

  • Do specs to test dekorator:install and decorator generators.
  • Generate a dummy_app one time, then duplicate it to do tests from scrash

Better benchmark

In #28 we implement a benchmark and did some optimization.

I think our benchmark could be improved. It the first time I did one, so I'm sure it's not perfect.

Additional SQL query for AR Relations

Hey there,

playing with dekorator for the first time. I was exploring the source code and found the following method:

def decorable?(object_or_enumerable)
return false if object_or_enumerable.respond_to?(:empty?) && object_or_enumerable.empty?
return false if !object_or_enumerable
return false if object_or_enumerable.is_a?(Dekorator::Base)
true
end

I'm interested in the design decision for object_or_enumerable.respond_to?(:empty?) && object_or_enumerable.empty?. I see how it can be useful for basic arrays (although iterating over an empty array isn't bad either), but when passing an AR Relation, that line will perform an additional query:

[1] pry(main)> Dekorator::Base.decorate(User.all)
  User Exists? (0.6ms)  SELECT 1 AS one FROM "users" LIMIT $1  [["LIMIT", 1]]
  User Load (0.4ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]

[2] pry(main)> Dekorator::Base.decorate(User.all.to_a)
  User Load (0.5ms)  SELECT "users".* FROM "users"

Is that intended behaviour?

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.