How to replace a Template using Deface - ruby-on-rails

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

Related

What is the process for locating any given gem's built-in scaffolding templates?

I'm tweaking some scaffold templates for an app so I mostly just want to copy the built-in/default templates. My question is what is the process I can follow to locate the templates?
For example, thanks to the helpful answers of Tim Krins I now know to look in:
cd $(bundle show railties)/lib/rails/generators/rails/scaffold_controller/templates/ for controller templates (reference)
cd $(bundle show activerecord)/lib/rails/generators/active_record/model/templates/ for model templates (reference)
I also managed to combine his bundle show <gem> tip with spelunking through Github issues in haml-rails to figure out the path for HAML templates is:
cd $(bundle show haml-rails)/lib/generators/haml/scaffold/templates
I haven't yet been able to use that knowledge to figure out the general solution though so next time I'll have to search StackOverflow again.
How would I go about figuring out for myself the template locations for various test files (without telling you which testing framework I'm using)? How would I locate the templates used for the invoke resource_route step?
How do I figure out which gem to look in? (e.g. How could I have figured out railties vs activerecord above?)
How do I figure out what path to look in? (e.g. How would I know to look in activerecord/.../activerecord but railties/.../rails?)
Which gem are you trying to change?
What is needed in the case of the Devise or Spree gems is to recreate the filepath within the app directory, then copy/clone the erb/haml file from the gem's github repo, and make your changes in the local file.
There is a gem called Deface which can allow you to edit specific html components in views without recreating the entire view. This is a more complex but ultimately more maintainable path - depending on the extent of your changes.

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

How can I change the default path to the `rails` script in my project, or remove that dependency?

In another question (Why does the Rails command force a help message for the new command?), I found that Rails needs the rails script to be in the script folder, in the root of my project, in order for it to be properly detected as an existing Rails projects, and allow me to use the various rails commands other then new.
I did this because I felt that the more popular moniker for including executable content in a repository to highlight available use cases is by using the name scripts. At least the pluralism in English should be appreciated!
Is there anyway to change which folder the main Rails executable looks for the project-included one?
I actually think it's a bit silly to include this rails executable in the project, and can be redundant. Maybe it's for customization, but I feel that could better be done in the configuration, environment, other .rb files. So also, could this just be removed somehow, and still have a functioning project through varied use of the main rails command.

How do I theme a Spree website?

I'm creating a spree e-commerce site and was wondering with the current updates to spree how to theme the default pages ( product, home page ) since I can't seem to find the files to do so.
You will need to create the views for the frontend. First understand the idea of how default views are shown.
The default views are located in spree-frontend gem. Check this out.
https://github.com/spree/spree/tree/master/frontend/app/views/spree
If you want to change the theme for only some parts, add the views for those parts only. If you want to change everthing, add all the views here.
For example, if you want to change the layout and the home page only, for layout you create
app/views/spree/layouts/spree_application.html.erb
and write your own layout.
for home page you add your html and template to the file
app/views/spree/home/index.html.erb
similarly you can change all the templates for the pages you want. That is how you change the theme.
Note:
you cannot find the files for the views because, the are located in the gem file.
Update I
To avoid having assets from gems, you can edit vendor/assets/javascripts/spree/frontend/all.js and vendor /assets/stylesheets/spree/frontend/all.css
for example to remove the default assets for spree_static_content you can remove the line
*= require spree/frontend/spree_static_content
Similarly you can also override the entire css and js, or partially
To replace an entire stylesheet as provided by Spree you simply need to create a file with the same name and save it to the corresponding path within your application’s or extension’s vendor/assets/stylesheets directory.
For example, to replace spree/frontend/all.css you would save the replacement to your_app/vendor/assets/stylesheets/spree/frontend/all.css.
This same method can also be used to override stylesheets provided by third-party extensions.
Its explained very well in details in:
spreecommerce' documentation
If you have any confusion with you, please comment and i can help you.

Alter views of a mounted engine [Monologue]

I want to alter the views of a mounted engine called Monologue
I've found this in the docs:
Monologue.layout = "layouts/monologue/application" # set the layout you want to use if you want to use your main_app layout
Which adds the main template, I'd rather change the engine views directly.
Where are those files located? I couldn't find them at the root of my app.
The previous answer is incorrect. Don't edit the gem files directly or your edits won't survive updates.
The correct way to modify the Monologue engine's files is to copy the gem's view files into the host application's /views.
For example, create application.html.erb inside host_app\app\views\layouts\monologue and edit that file. Your rails app will hit the custom view file and instead of the default ones in the gem. You can customize any Monologue view or partial in the same way, by simply duplicating Monologue's path in your application's views.
See all the files you can modify in this section of the Monologue github repo.
Edit: More information on customizing Monologue can be found here.
Gem files are located in .rvm or .rbenv folders which ever you are using.
rbenv:
$HOME_DIR/.rbenv/versions/#version_no/lib/ruby/gems
rvm:
$HOME_DIR/.rvm/gems/ruby-version

Resources