Expose function in ruby on rails - ruby-on-rails-3

I am looking at a rails app and at the top of every controller there is a block of code that looks something like this
expose(:var) {Model.find params[:var_id]}
I understand what is inside the block just fine but...
I cannot find any documentation on what the expose function does where it comes from or anything I have tried searching the project and using the searchable rails docs.
I would love to know what it does, can someone please tell me or point me to the docs.

This is probably referencing the decent_exposure gem. You can learn more about it here: http://railscasts.com/episodes/259-decent-exposure
Source: https://github.com/voxdolo/decent_exposure

It's a method from the Decent Exposure gem. You can check out a screencast that Ryan Bates did on it over at Railscasts. It's a really great gem. I use it in my application. It cuts down on a lot of the redundancy in the controller layer.

expose is not a part of Rails, it comes from the decent_exposure gem.
It is not an answer to the question. I just want to make the Rails world a bit better and I hope that somebody will read this.
Please think twice before using expose. You should only use it if you 100% sure you are using it the right way and it really makes the code better. Read the documentation properly!
One of projects I worked on became unmaintainable because of tons of expose in controllers which replaced not only all #instance_variables passed to views, but also a lot of business logic and the most helper methods.
When you use expose it is not clear in which controller actions and in which views it is used. Unexperienced developers combine data and logic for multiple actions and multiple views in the same expose block.
That's a nightmare.
Believe me, expose really destroys projects if not used properly.

Related

Is it possible to use Aurelia in a non-spa application?

Recently I've been playing around with different frameworks and libraries, looking for something that really suits my needs.
You see, my job mainly involves creating asp.net mvc applications and for most of them using Razor and a little bit of jQuery is enough. But in certain cases and only for a few pages,which are rarely more than one or two per app, I really need something extra that helps me avoid getting entangled in a bunch of jQuery code.
As I mentioned, I tried a couple of alternatives and from them, the one I liked the most is Aurelia, because of its simplicity and the fact that it embraces standards, BUT the more I dive into the framework, the more I think that it might not be what I'm looking for,as it seems more suitable for full spa applications and what I need is:
Something that helps me reduce the amount of DOM manipulation
A efficient templating engine
I know that Aurelia provides that and much more, but I don't want/need a SPA, I need those functionalities ONLY in some specific pages and not the whole application.
Can Aurelia help me achieve this? If so, how?
Sure, Aurelia can help you achieve that. You just won't use certain features like routing in on the pages you create with Aurelia.
That being said, it isn't a drop in replacement for jQuery, but none of the "modern" JS frameworks really are. And you're going to end up spending time learning whichever one you end up choosing.
Check out the aurelia.enhance functionality, it might be just what you're looking for!
I have used Aurelia in a non-SPA context, and it worked out well. I think this is exactly what you describe. For example:
http://legumeinfo.org/chado_phylotree/phytozome_10_2.59028020
https://github.com/legumeinfo/tripal_phylotree/tree/lis_master/theme/js/aurelia
I'm using aurelia for dynamic elements on some sites. Like comments for example. Page loads fast w/o comments.Then Aurelia kicks in and loads the comments below. Also with some signalR magic the discussion is updated in real time. It is awesome and insanely easy.

Is there a workaround for strong parameter hell?

I just inherited a RoR 3.2 app and am trying to get it working on 4.2
I am going to put on my sarcastic hat for a second, just so I can feel better. Instead of having a single line in a single file to protect specific fields from mass-assignment, "Strong" Parameters requires bloating up controllers and heaven help you if a controller uses multiple models or a model is used by multiple controllers or need nested attribute whitelisting. This is the exact opposite of DRY and KISS.
That is better. Okay, so the question is, besides getting rid of mass assignment completely, which is sounding really good right about now, is there a sane way to use it or get around it. From what I understand that gem that brings back attr_accessible won't work in Rails 5 which is where this app is heading.
I understand the Ruby object model and can make a ton of modules that controllers can mixin, but that is just ugly and still error prone.
Any advice or hints would be welcome.
Why is that every new Rails feature involves more boilerplate spread over multiple files? If I wanted Java, I know where to find it. The stupidity of getting rid of the powerful and clean link_to_function in favor of using a tangled mess of callbacks almost made me quit, but adding that function back is trivial. Hopefully when the client wants the inevitable upgrade to Rails 5 I can talk him into something more sane and move things bit by bit to a sane web framework.
What you're after is "form objects". There's a great railscast episode on them. You may also be interested in the reform gem
Edit: looks like there's a free version of that railscast episode on youtube: https://www.youtube.com/watch?v=SvL_aZt3zyU

ROAR: What are the benefits over jbuilder or rabl?

I've read through https://github.com/apotonick/roar and it seems like a lot of thought has been put into ROAR. But in the context of a fairly standard Rails-powered JSON API that uses jbuilder, I'm still not able to immediately see what benefits ROAR provides.
What am I missing?
Within a Rails project, you should do as much as possible to stick with the built-in solution, but outside a choice is mandatory. Personally one aspect that I love about ROAR is the versioning for the API.
There is one good post I like that might help you:
http://devblog.reverb.com/post/47197560134/hal-siren-rabl-roar-garner-building-hypermedia
It might not be an straight answer, but it explains the problems you should be aware of when choosing a JSON API generator.

Advanced Ajax with RoR and jQuery UI

I don't have much web development practical experience although I had studied a lot on the web (Treehouse, CodeSchool, other tutorials and articles).
I have found some explanations on how to use Ajax with RoR and jQuery. But I'm having difficulties to understand how to solve my problem as the explanations often use jquery-ujs that I suppose I can't use in my case.
My problem is difficult to explain here but is similar to implementing this shopping cart on RoR, including more parameters like price
The jQuery UI is working for me in my RoR application. What is missing is the AJAX part and how to make it work with RoR
I have found some questions here on StackOverflow that is about this topic but it didn't helped much as they are probably for more advanced users.
So, I believe that the best help would be a tutorial or book or some other reference that is more similar to my problem. However any advice or other kind of directions may help
After struggling a lot, finally I solved my problem.
I will post here the resources and some tricks that helped me. I hope it can help others. StackOverflow didn't let me post all the links. For this reason, I just gave some hints that may help to find some resources.
About the jQuery Ajax concepts, I recommend Code School jquery course "The Return Flight".
The second thing that I think helps is learn to debug. Firebug helped me a log to debug the client side/javascript/Ajax. This link may help on that:
http://www.zyxware.com/articles/2642/debugging-tips-how-to-debug-ajax-requests-using-mozilla-firefox-and-firebug
Since the problem may be to debug RoR. Look for a RailCast on the subject.
One of the things that I took some time is if it would be better to use $.ajax(), $.post(), $.get() or $.getJSON(). The link below may explain better but to sum it up, $.ajax() is the more generic one and the other are simpler ones and that call $.ajax() with specific parameters.
Difference between $.post and $.ajax?
It is important to understand how RoR routing works to call the right url with the right $.ajax() type. "rake routes" may help.
This link may also help to use the right $.ajax() type.
http://old.thoughtsincomputation.com/posts/understanding-rest-in-rails-3
Finally, to send the right format from the javascript to RoR controller, this link may help:
How to send and store data to rails through jquery/ajax as a JSON object?

How to do enumerations in Rails 3?

In Rails 2 I know of a few plugins for enumerations such as acts_as_enumeration and enumerate_by but they don't seem to be maintained or updated for Rails 3. Preferably, the solution would store the enum in memory rather than a database for performance reasons but really any method would be useful since it can always be cached.
I did find enumerated_attribute that claims to work with Rails 3 but quite honestly I don't like the API and was hoping for another good solution.
(Sorry for only linking to the one plugin but it won't let me post more than one link until I get a higher reputation)
I am currently using lwe's simple_enum which seems to be actively developed and stores values on memory or if you prefer on a table.
If you're using DataMapper as your ORM have a look at dm-types which includes an Enum type.
There is this gem, enumerate_it, it has good documentation and very well done!