Coder Social home page Coder Social logo

Comments (10)

kimptoc avatar kimptoc commented on July 19, 2024 1

Hi,

Thanks for the options, just trying them now.

On the first option with the lambda, "m" is the model object, right - but what if we are using it in the scenario of "object false"...

Tried this, but it always excludes the field now :(

node(:message, :if => lambda { |m| @message.nil? }) { @message }

With option 2, I am probably just doing it wrong.

My helper is this:

module RablHelper

  def node_if_present(name, &block)
    node(name, { :if => lambda { |m| m.send(name) } }, &block)
  end

end

And this in the view:

node_if_present(:message) { @message }

But get this error:

    undefined method `node' for #<#<Class:0x106837fa0>:0x1068354a8>
    Extracted source (around line #2):

    1: object false
    2: node(:request_id) { @request_id }
    3: node(:status) { @status }
    4: node_if_present(:message) { @message }
    Rails.root: /Users/kimptoc/Documents/ruby/ecom/ecom1

Application Trace | Framework Trace | Full Trace
/Users/kimptoc/.rvm/gems/ree-1.8.7-2011.03@p-ecom1-rails3-wsse/bundler/gems/rabl-7f97b7e60aee/lib/rabl/engine.rb:20:in `render'
/Users/kimptoc/.rvm/gems/ree-1.8.7-2011.03@p-ecom1-rails3-wsse/bundler/gems/rabl-7f97b7e60aee/lib/rabl/engine.rb:162:in `send'
/Users/kimptoc/.rvm/gems/ree-1.8.7-2011.03@p-ecom1-rails3-wsse/bundler/gems/rabl-7f97b7e60aee/lib/rabl/engine.rb:162:in `method_missing'
(eval):4:in `render'
app/views/ws/new_quote.rabl:2:in `_app_views_ws_new_quote_rabl___1547417618_2202114200_0'
actionpack (3.0.7) lib/action_view/template.rb:135:in `send'
actionpack (3.0.7) lib/action_view/template.rb:135:in `render'
activesupport (3.0.7) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.7) lib/action_view/template.rb:127:in `render'
actionpack (3.0.7) lib/action_view/render/rendering.rb:59:in `_render_template'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'

from rabl.

nesquena avatar nesquena commented on July 19, 2024

Generally I like to include nil values so the key still exists consistently in the API but obviously it depends on the use case. The only way right now is using the if syntax to check. I could see wanting it globally but to me this seems more like a case by case setting...

node(:foo, :if => lambda { |m| m.foo }) { ... }

Keep in mind helpers work in rabl as well so you could add a shortcut in theory...a helper that returns a proc.

from rabl.

nesquena avatar nesquena commented on July 19, 2024

I just realized helpers open up a whole slew of extensions you could build for instance, in a helper you could theoretically do:

# app/helpers/rabl_helper.rb
def node_unless_nil(name, &block)
  node(name, { :if => lambda { |m| m.send(name) } }, &block)
end

and then use that in your template:

node_unless_nil(:foo) { ... }

from rabl.

nesquena avatar nesquena commented on July 19, 2024

It looks like you have the logic reversed, try:

node(:message, :unless => lambda { |m| @message.nil? }) { @message }

Note in your example you said to display the message only if the message is nil which doesn't make sense.

As for the second example, you would need to include the helper directly into the template, just for fun try:

include RablHelpers
node_if_present(:message) { @message }

or to include special helpers in every template:

RABL::Engine.send(:include, RablHelpers)

I would probably just go with the direct "if" approach for now.

from rabl.

kimptoc avatar kimptoc commented on July 19, 2024

Hi,

Just tried this - the first, inline option, works now - I had been using ".present?" and tried switching to ".nil?" to see if it made a difference... guess I gave up too quick :(

Still cant get the helper to work.

  • if I use the Engine.send option, its not finding RABL:: ... - I tried at the end of initializer and directly in the controller.
  • If I include it in the template, I get undefined method "include"

Dont suppose its a new feature that I need to update the gem for?

Thanks,
Chris

from rabl.

nesquena avatar nesquena commented on July 19, 2024

Ok try this:

helper RablHelpers
node_if_present(:message) { @message }

and see if that works. I am aircoding these so it is likely my examples are just wrong in some cases. This one might work, let me know :)

from rabl.

kimptoc avatar kimptoc commented on July 19, 2024

Hi,

Getting closer - the helper is being loaded, but the call in the helper is failing :(

kimptoc/rabl_examples@8360ef2

Thanks, Chris.

from rabl.

nesquena avatar nesquena commented on July 19, 2024

What about just:

node(:message) { @message } if @message

I am sure it is close with the helper, but I am not sure exactly what you are after for it to do, my version was more of an example. I am sure you could get it working, it just depends on what you want the method to do.

from rabl.

kimptoc avatar kimptoc commented on July 19, 2024

Thats simpler/clearer - nice. Thanks.

from rabl.

oharsta avatar oharsta commented on July 19, 2024

I realise this thread is very old, but it was the first one popping up on my google search "rabl exclude nils" so for future reference: I have created a pull request for excluding nil values globally: #514

from rabl.

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.