Change spree application layout file to load - ruby-on-rails

Is it possible to use my own application layout file in my main rails app instead of spree's? I have my main app already setup with devise and then added spree. Spree frontend uses
frontend/app/views/spree/layouts/spree_application.html.erb
in its own gem as its layout and I read how to override that file with my own in app/overrides or Deface, but I don't want to duplicate the content that is already in my app/views/layouts/application.html.erb.
I'm looking to use my own file instead or overriding spree's.

You have 2 options:
Create a app/view/spree/layouts/spree_application.html.erb in your rails app and rails will pick your file
Set your own layout with Spree::Config[:layout] in an initializer (ex: Spree::Config[:layout]='application')

Spree::Config[:layout]='application' worked for me. But when I tried to switch to the spree default layout by removing this line, it did not work. After spending almost an hour I was able to switch back to default Spree::Config[:layout]='spree/layouts/spree_application'
Adding this answer just in case someone else gets stuck with similiar error.

Related

Is it possible to override the create_migration template in Rails 6?

I tried placing my custom file in multiple locations inside the lib folder with no success.
I found a PR from 2017 to reactivate this behavior but wasn't merged.
https://github.com/rails/rails/pull/13972
I had a look at
https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb#L17
and it looks like it's not implemented, but I don't know if just this file should change.
I´ve found resources on how to do it in older rails versions, but is there any way to override the Rails templates in Rails 6?
From this line of the documentation:
Since we want to customize Rails::Generators::HelperGenerator, we can do that by simply making a template copy inside lib/templates/rails/helper with the name helper.rb.
In the case for migrations, the template names should be specified as follows (here are the default templates for reference):
lib/templates/active_record/migration/create_table_migration.rb.tt
lib/templates/active_record/migration/migration.rb.tt

Overriding the default views in RailsAdmin

I am using Ruby on Rails 4.2.1 with RailsAdmin. The gem works excellent, but I have the requirement that the layout of the admin panel and the forms must look different than what is generated by default. For example, the navigation should be horizontal top, the forms should order the fields in two columns.
So far I haven't find a way to copy the views locally and modify them (like in Devise for example). I have tried to replicate the views manually in the respective path under my views folder by copying the original views, but I got problems with the helper methods that are part of RailsAdmin not being accessible from my views.
I dug deeper and found that there is a task copy_views, it was referred to in questions for the older versions of the gem, but if I try to use it now rake rails_admin:copy_views, it is not available anymore.
Am I doing something wrong, or is there another way to do this?
You can create folders in your app
app/views/rails_admin/main for https://github.com/sferik/rails_admin/tree/master/app/views/rails_admin/main
app/views/layouts/rails_admin/ for
https://github.com/sferik/rails_admin/tree/master/app/views/layouts/rails_admin
Put modified files there. It can get a little messy and you will need to update the files if the gem changes.

How to replace a Template using Deface

I'm new to rails and I've set up a standard spree e commerce solution. I now want to customize the default templates and styles. According to the spree documentation [1], the best way to do so is using Deface. Unfortunately, I can't figure out the location of the existing views/layouts.
For example, while the document suggests:
For example, to override the main layout, create the file YOUR_SITE_OR_EXTENSION/app/views/spree/layouts/spree_application.html.erb
my app/views folder doesn't contain a folder called spree
Can anybody tell me, how to change the view templates?
Thank you.
[1] https://guides.spreecommerce.com/developer/view.html
You have to actually create that directory and every directory along the way until you reach the file you wish to override. If you run these commands from your root directory you should be good to go.
mkdir app/views/spree
mkdir app/views/spree/layouts
touch app/views/spree/layouts/application.html.erb
Then copy the content from here: https://github.com/spree/spree/blob/master/frontend/app/views/spree/layouts/spree_application.html.erb into the file you've just created.
If you restart your web server you should be able to make changes to the local application.html.erb file and see them locally.
Any time you want to override a template completely, you can find the template you need to override on Github: https://github.com/spree/spree and recreate the necessary files/directories. Spree will look for templates in on localhost before the gem, so as long as you override the right template in the right directory, you can customize any view you like.
Alternatively, you can try running the rails generate spree:frontend:copy_views command to pull all the views you need from gems, and anything that is missing can be added manually, in strict accordance with the gems folder hierarchy. For example, in gems is parsl in the following spree_backend/views/spree/admin/users/_lifetime_stats.html.erb. To enter the desired code, I have to make a folder on the following path - the name of the project/app/views/spree/admin/users/_lifetime_stats.html.the erb and all the changes I will make here will fall into the main view code. This is an alternative to Deface::override

How do I stop caching of layout in production with Ruby on Rails?

I'm building a rails app allows the admin to load (and reload) an .erb template that will function as a layout. i.e. the application.html.erb
On occasion when updating an already created template to my hosted app the changes do not take effect. If I load a differently named file to the same template this does show the changes.
This is not an issue in DEV on my local environment but is an issue for PROD. Is there a setting that controls the caching of the layout for a page? The dynamic content is no problem but the static layout does not change unless I restart the app. Please let me know if you have any brilliant insights about this issue, or if it needs greater explanation.
Just figured it out - add this to your production.rb environment file;
config.action_view.cache_template_loading = false

changing complete layout in spree

I am working on spree 1.0.0 and have been doing some research on it
for my e-commerce site.
I have already made and used some existing extensions in my app. Now,
I am working on layouts and have been trying to figure out that what
are the best possible ways for that.
My need is that I have to change the complete front end layout of my
store in comparison of what spree provides.
Some of the options from my point of view are
-> Use an extension to write all the views that overrides the templates that spree provides,
-> Use Deface to override views (which would be hectic as I have to change approx everything on almost every page)
-> Use mixed functionality of the above options.
or is there another way to do this.
thanks in advance.
You are likely to have a new issue soon: to tell your new controller to use your new spree_application layout.
The spree google group indicates that you can use inheritance to use the main application everywhere: https://groups.google.com/forum/?fromgroups#!topic/spree-user/mB02WqMnCnw%5B1-25%5D
However, I still haven't figured out how to solve the routing for those controllers.
You can do this by overriding the app/views/layouts/spree_application.html.erb by placing an identically named file inside your application's app/views directory.

Resources