Comments (4)
Hey, it works with adding require 'fortitude/rails/helpers'
at the top! But still feels weird.
from fortitude.
Ah! So, the simple part first — you should be able to just call helper
on any widget class directly, and, if you do so, you shouldn’t need that require
. For example:
class Views::Base < Fortitude::Widget
helper :simple_form_for, :transform => :output_return_value, …
end
(You could even do this on Fortitude::Widget
, if you wanted.)
If you run into problems with this, let me know, but it ought to work just fine. I see specs for this stuff and they're passing (although I can always believe I screwed something up!). The output_yielded_methods
stuff is trickier. I built it for form_for
(and fields_for
). Let me see if I can explain it with a comparison.
(this part, I’m sure you’re aware of) Almost all Rails helpers return data, rather than output it; calling text_field_tag :foo, …
returns the HTML for a text field, rather than rendering it. But this would mean having to write, in Fortitude, rawtext(text_field_tag :foo, …)
, which is really ugly. So I added the :transform => :output_return_value
transformer, which outputs the return value of the helper, so we can just write text_field_tag :foo, …
, like we want to.
(the even trickier part) form_for
(and fields_for
) is the difficult one. ERb usage is:
<%= form_for @my_model do |f| %>
Bar: <%= f.text_field :bar, … %>
Baz: <%= f.text_field :baz, … %>
<% end %>
form_for
itself actually returns a little HTML itself (the <form>
tag), but, more importantly, it yields something to its block that, when you call methods like text_field
on it, returns HTML of various forms. If all we did was declare it with the :transform => :output_return_value
trick, we could write just form_for …
in Fortitude and get the <form>
tag just fine — but the calls to (e.g.) f.text_field
would just be returning HTML that got thrown away, unless we wrote rawtext(f.text_field :bar, …)
, which, again, is ugly.
The :output_yielded_methods
magic actually takes the object that the block yields (the f
in the above example) and wraps it in another object (an instance of Fortitude::Rails::YieldedObjectOutputter
) that simply passes through all calls to the underlying f
…except that if they’re in the named list of methods you provide, it renders their return value on the way out. This is what allows us to write form_for
in the “idiomatically correct” way in Fortitude, and not have to worry about sprinkling rawtext
calls in various places. It should also let you use simple_form_for
(which we use at my current employer, as well), too. So you end up with Fortitude code like:
form_for @my_model do |f|
text "Bar: "
f.text_field :bar, …
text "Baz: "
f.text_field :baz, …
end
…which, at least to me, feels “natural” and right.
(Oh, and by the way, with all this stuff, any time we automatically output the return value of any method, inside a block or otherwise, we replace its return value with ''
— so that if you still wrap it in rawtext(…)
, it doesn’t harm anything.)
Hope I explained that at least sort-of reasonably well! Let me know if it’s not clear. It’s definitely one of the more voodoo corners of the Fortitude codebase, although I’m pretty happy with its net effect for users, too — it makes this stuff “just work” the way you expect it to work, instead of having to think carefully about output vs. return.
from fortitude.
Thanks for the great reply. I'm familiar with this stuff from the Erector codebase too, where it was a bit of a mess. (https://github.com/ajb/erector-rails4/issues/16, https://github.com/ajb/erector-rails4/issues/31, etc.)
Your implementation was a bit harder to grok at first glance, but it looks like it will work a whole lot better. Thanks again for the detailed explanation!
from fortitude.
Yeah, totally. As I write docs, I'm planning to do user-facing and code comments at the same time, exactly for bits of implementation like this that are a bit impenetrable. That should hopefully make contributing to the code base a whole lot easier.
from fortitude.
Related Issues (20)
- Unit testing of widgets without loading entire Rails stack in test environment? HOT 2
- Script to convert to and from HTML/ERb? HOT 3
- :csrf_meta_tags helper unavailable? HOT 1
- Even more forms weirdness, or... HOT 11
- Using #inline_html with a Rails view context HOT 2
- Implicit passing of `needs` variables fails when View class is named the same as the `needs` key HOT 4
- Can't mark HTML attribute values as html_safe HOT 1
- Upgraded to Rails 4.2.5.2 and everything is blowing up HOT 3
- use controller.view_paths to resolve view paths when calculating widget_class_from_file HOT 1
- Status? HOT 8
- alias_method_chain deprecated in Rails 5 HOT 2
- fix Travis build HOT 2
- Release latest code to rubygems HOT 1
- ArgumentError: wrong number of arguments (given 5, expected 4) with ActionView 4.2.6 HOT 2
- Issues when running tests: "uninitialized constant OopRailsServer::RailsServer::Bundler" HOT 1
- Regression: automatic inclusion of Rails helpers breaks implicit route arguments HOT 6
- Fortitude prevents Rails csrf_meta_tags from being rendered in the <head> element HOT 1
- retina_images repo unavailable
- Single arity template handlers are deprecated in Rails 6.0
- Still maintained? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fortitude.