How can I add my custom methods to my assets such as css files like Rails did with 'asset_path' helper?
With Rail's own helper, I can write this:
# some.css.erb:
<%= asset_path 'a_image.png' %>
# How can I write this:
<%= my_custom_method 'a_image.png' %>
I've tried many ways but couldn't found a decent way to this. Do you know one?
Thanks
The best way I found was to create a custom helper module in app/helpers:
module AssetsHelper
def my_custom_helper_method
# do something
end
end
And then to require it like this in application.rb, after your applications configuration (very bottom):
module Sprockets::Helpers::RailsHelper
require Rails.root.join('app', 'helpers', 'assets_helper.rb')
include AssetsHelper
end
And you might follow this issue to find a better way: https://github.com/rails/rails/issues/3282
Normal helper methods are not available in asset views. To add you own methods you'll need to extend the Sprockets helper module. Have a look at the code of the built-in helpers to see how you might do this.
In a nutshell you can add a file in lib with the same structure as this and add you own methods. Don't forget to include the new library in you application initializer.
Related
I'm using Middleman 3.4.0 and in customizing my project I have some methods that are becoming too extensive for keeping them as helpers in config.rb. I'd like to write either a Module or Class and mix these back in as helpers with the ability to model the objects I need to work with. Is there a pattern for writing this as a Middleman extension somewhere that I can start from?
Use the middleman extension <extension name> command, which gives you a good boilerplate, see here.
I have a number of mailers in my Rails 3.2 application, and I would like all of them to use the same email layout. That layout includes a header and footer, which includes images to be used inline. I'd like to follow the DRY principle of Rails, but I'm not entirely sure what the best practices are for this.
As this answer points out, I can use AbstractController::Callbacks to create a before_filter to add inline attachments. But how do I do this as well as set the layout in one module that I can then include in the mailers?
Ideas I have at the moment are to create a new mailer class called DefaultMailer, then have all mailers that I want to use the layout inherit from that class. Or to create a concern that would handle these tasks. Before attempting to hack something together using one of those techniques, I thought I would ask here to see if anyone had done this successfully before to help guide me.
AFAIK there are some issues when inheriting from other mailers (ie. not inheriting the settings).
Common solution is to create a mailer base module. This is a snippet from existing project:
module MailerBase
extend ActiveSupport::Concern
included do
helper :application
layout 'mailer'
default from: "#{AppConfig.application_name} <#{AppConfig.mailer_sender}>"
end
end
You can combine this with your callbacks, although including remote images is more common and arguably better solution.
The default ruby on rails scaffolds generate a new.haml or new.erb these include only 1 line: = render 'form'
Thus, basically the file is obsolete, is there any preferred way to remove the new partial and make it use the _form partial instead straight away or would this go against rails conventions?
no, there is no preferred way. it goes against rails conventions.
TL;DR don't do it.
if you don't need any customizations on your templates and are just creating basic CRUD stuff, have a look at admin tools like RailsAdmin ActiveAdmin Typus etc.
I would like to write a helper that will only be used in my spec files and I do not know where this code should go.
I am not trying to test an app helper and I do not feel like creating an app helper that will only be used for testing.
What is the good practice?
Thanks,
Typically I put these in spec/support/spec_helpers. I then include those modules in the appropriate examples.
If you have some that are useful to all specs of a certain type (e.g. all request specs) then you can do
config.include SomeHelper, :type => :request
which will include that module into all your request example groups.
Is it possible to configure rubycomplete.vim that popup scope methods at the top and gather them from models files first, than from global declaration of ActiveRecord methods
If you are using tpopes awesome rails.vim in conjunction with exuberant ctags you can simply run the command :Rtags and it will generate all relevant tags for your project.