How to move back from inherited resources gem - ruby-on-rails

Recently I scaffold-ed two of my models/controllers/view, let's call them xxx and yyy. Now I look under the controller file, I see absolutely nothing ! but it was still functioning, upon investigation I found that was due to the
inherited_resources Gem
So the controllers look like this currently
class xxx < InheritedResources::Base
end
so if I change
InheritedResources::Base to ApplicationController
like I have it other controller, would it behave like normal controller ? I tried looking up on the docs, I was referred here for questions.
What is the best way to get normal controller/models back for those two scaffolds ?
Thanks for your time and help.

in config/application.rb add:
#use rails scaffolding generator
config.app_generators.scaffold_controller = :scaffold_controller

When you use InheritedResources, the gem registers a controller generator that generates just that, your controller definition. The point of using InheritedResources::Base is to clean your controllers and move all of the seven REST actions to a common class. You don't need to define any of the following methods if you extend InheritedResources::Base
index
new
create
edit
update
show
destroy
They're all the defined for you. Go ahead and make a test, scaffold a resource and go to it's index method, add a couple records, play around...
Now if you really want to go back to the old way and have your code generated by the bundled controller generator, remove inherited_resources from your Gemfile, bundle install, and generate your scaffolds again.
Hope it helps (:
P.S. if you decide to use InheritedResources (which I suggest you do) you can overwrite any methods you want to customize. Have a look at the docs, everything is more clear over there.

Related

How can I disambiguate my own class from a gem's class

I have a Rails project that I have been working on for a while. Like many Rails projects I have a User class. In one of my controllers I need to access some methods from a gem I am using. The example code from the gem demonstrates using an include to a particular gem module. I'm not going to use the actual gem here because it is not important to the question. I have no control over this gem and I need functionality from the module.
include GemName::Module
The problem is that the Gem includes its own User class directly under the module I have included in my controller. This results in calls to my own User class to methods not defined in the gem's user class failing:
User.working?(:test_user)
NoMethodError (undefined method `working?' for GemName::Module::User:Class):
What I would like to do is be able to include the module in my controller and be able to use my application's User class in that same controller.
I have tested the following workarounds, both of which seem to work, neither of which I am particularly happy with:
Create a new constant to refer to my own user class before including the gem module.
LocalUser = User
include GemName::Module
Do not include the module and explicitly call any classes I may need with the fully namespaced call.
GemName::Module::Class.method
I realize I could namespace my User class but that would involve a lot a refactoring around my codebase and I don't really love the idea anyway. This controller calls classes from the gem about 20 times and my own User class exactly once. The controller never calls the gem's User class. If possible, I'd love to force the call to my own User class to refer to the my non namespaced class explicitly and keep the include to the gem module.
Hopefully there is an elegant solution which will increase my understanding of namespaces in Ruby
Just after posting the question, I thought "What if I simply add :: in front of User when calling my own non-namespaced class?"
Sure enough, it works.
::User.working?(:test_user)
Calls my own User class.
I'm leaving the question and this answer up in case it helps others.

rails don't calls the engine's controller

I am trying to define some helper methods to be used in the app's controller, but it seems that rails don't even call the controller. just for the test I have the following controller in my app/controllers/my_engine/application_controller.rb and as the documents say rails should find it first and an error should raise because THIS_SHOULD_PRODUCE_ERROR is unknown, but the rspec happily executing without any errors!
class ApplicationController < ActionController::Base
THIS_SHOULD_PRODUCE_ERROR
end
I even tried to mimic the devise's way but the results are the same!
The guide section on the app directory suggests that the application_controller in an engine "will provide any common functionality for the controllers of the engine".
So I wouldn't expect that any additions to that controller will be available to all controllers in an application.
That also means that your application_controller is, I suspect, not getting called when you're running your test. Which would explain why you're not seeing an error.
In terms of how devise does it I think you need to be looking at how define_helpers works. The code you've linked to in your question is the application controller in the test app for the devise gem.
I noticed that I have got things wrong, and the application_controller in the engine does not get applied to application_controller in the app! Also, I couldn't figure out how the devise did it, but I have come up with the simple workaround for this:
require_relative 'controllers/helpers'
module Acu
module Injectors
class << self
ActiveSupport::Notifications.subscribe "start_processing.action_controller" do |**args|
eval((Acu::Configs.get :base_controller).to_s).class_eval do
include Acu::Controllers::Helpers
end
end
end
end
end
This will inject controller helpers to the user's base controller (which I get from the user, default: :ApplicationController) at the end of the class, which is perfect for me (but don't know how to add it to begging of the class if anyone needs it)

Rails & Refinery CMS: Determine the Controller class name from view?

I'm working with an existing Refinery CMS app for a client that has many controllers in many different places. If you are n00b to Refinery CMS, you can nest entire rails apps INSIDE the vender folder and they act like plugins. Its complex how it works and even worse a lot of the models/controllers are embedded in the refinery gem so a controller might exist but theres not file for it.
I wanted to extend a controller by following this example:
http://refinerycms.com/edge-guides/extending-controllers-and-models-with-decorators
which I did but my code was not firing. I did actually fix this so my problem is solved but in the future it would be useful to know what controller called this view I have. The view is tucked away in the gem HOWEVER a partial that it references was already overridden so I could throw something like:
<%= raise self.class.to_yaml %>
The problem with this I get the following error:
can't dump anonymous class: #<Class:0x000000061f5850>
Which isn't very helpful.
My question is this: How can I output the class name of the controller that calls any given view/partial ?
Thanks!
You can use params[:controller]
And params[:action] for current action

Ruby on Rails: partial view inheritance

I want to get next thing...
# For ArticlesController > ApplicationController
# in view
render 'articles/edit/form'
# tries 'app/views/articles/edit/_form.html.erb'
# then tries 'app/views/articles/_form.html.erb'
# then what it wants
Or maybe render with array partial option:
# For ArticlesController > ApplicationController
# in view
render_exists ['articles/edit/form', 'articles/new/form']
# tries 'app/views/articles/edit/_form.html.erb'
# then tries 'app/views/articles/new/_form.html.erb'
# then what it wants
This isn't realized, is this? But maybe some gems for 3.2 or monkeypatches... And don't you know pull requests to rails about it? Thanks!
UPD That's isn't controller-based view inheritance. This should work for (at the same page):
render `articles/edit/form`
render `comments/edit/form`
I'm using the mechanism I described in an article in the rails forum
It works a treat for me though I hear there is now some built in support in the latest versions or at least effort is under way to do add such a feature.
That already exists, and it's very similar to the controller inheritance.
You must follow a conventional strategy, however. You would put your global partial in app/views/application, then you can put a more specific one at each inherited level, like app/views/articles.
Take a look at the following railscast for more details: #269 Template Inheritance

Customizing/Overriding Rails SimpleForm Gem

I'm using the Rails gem SimpleForm, but I think my question may be applicable to any gem.
https://github.com/plataformatec/simple_form
It has a lot of great features and customization, but I'm looking to go a bit further. For example, I really wish the markup generated had no default classes inserted into it, but I'd still like the ability to insert my own manually. I found that I could remove some of the classes by commenting out lines in the gem files. However this is outside of my project-- I would want a DRY solution that will stay with my project when I deploy to production, preferably without having to pack all of my gems.
I imagine this is a common situation that could apply to any gem, and I should be able to override any gem wholly or partially probably by adding customs files in my project that override the gem... but I'm not sure how.
Any help would be appreciated! Thanks.
Are you talking about monkey patching? Say your gem has a class in a file
# simple_form_gem/lib/some_file.rb
class A
def some_method
puts 'A'
end
end
If you want to change the output of #some_method then you can create an initializer file and do
# config/initializers/my_monkey_patch_for_simple_form_gem.rb
class A
def some_method
puts 'duck punching'
end
end
Your monkey patch will only affect A#some_method, and not other methods in A. Just make sure the output of your monkey patch won't break something else in the gem.

Resources