GithubHelp home page GithubHelp logo

Comments (4)

tden012 avatar tden012 commented on August 22, 2024 12

This comment is a bit late, but I thought I'd add a solution that didn't involve forking. As mentioned by angelim, it's possible to create an ApplicationDecorator in order to add the methods necessary to add decorator methods to json. This is what mine looks like (it's more or less a complete copy of angelim's code)

class ApplicationDecorator < Draper::Decorator
  def as_json(options = {})
    as_decorated_json(options)
  end

  def to_json(options = {})
    as_json(options)
  end

  def as_decorated_json(options = {})
    model.as_json(options).merge(decorated_attributes(options))
  end

  def decorated_attributes(options = {})
    method_names = Array.wrap(options[:decorated_methods]).map { |n| n.to_s if respond_to?(n.to_s) }.compact
    Hash[method_names.map { |n| [n, send(n)] }].tap do |attrs|
      decorate_relations(attrs, options) if options[:decorated_include]
    end
  end

  def add_decorated_methods(options, method_names = [])
    options[:decorated_methods] = options[:decorated_methods] ? options[:decorated_methods] : []
    options[:decorated_methods].concat method_names
  end
end

Note the last method. This is useful if you want to make the model always render certain decorator methods in json. E.g.

class ModelDecorator < ApplicationDecorator
  delegate_all

  def my_decorator_method
    names[0]
  end

  def as_json(options = {})
    add_decorated_methods options, [:my_decorator_method]
    super options
  end
end

from draper.

steveklabnik avatar steveklabnik commented on August 22, 2024

This seems like pretty good advice, but it's not really an issue. How about adding a section about this to the README?

Since this isn't exactly a bug, I'm closing. Still good to have this text here, at least, thanks for writing it up. Always good to help future Googlers.

from draper.

kbaum avatar kbaum commented on August 22, 2024

I would like to add my decorator methods to the generated json. Is the best way to do that by using the fork from @angelim?

thx!

from draper.

steveklabnik avatar steveklabnik commented on August 22, 2024

I believe so, yes.

from draper.

Related Issues (20)

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.