Rails overriding Mini-Test templates - ruby-on-rails

I am trying to override the generator templates here, but am having no luck.
I have configured mini-test to use the spec syntax, and it generates the spec template but not my custom one. Also it calls the generated file for example *controller_test.rb even though it contains the spec syntax.
Here is my structure:

OK it turns out that the mini-test folder should be mini_test

Related

Rails partial generator of template

Is there a way in rails, that I can only generate templates partially?
For example in my generators/generatorName/templates folder, there is template1.html.erb and template2.html.erb files
in console when I run the command
rails g generatorName
It will just generate by default the template1,html.erb.
and if I want to add the another template, how can I do that? Is there a way?
Sounds like you're looking for a custom generator. You'll need to create/copy the second file on invoke.
http://guides.rubyonrails.org/generators.html

How can I pre-process .rb files in Rails?

I wrote a Ruby language pre-processor that I also want to tie in to Rails.
Accomplishing this isn't exactly clear from my research. I want the same effect of having .rb.erb or .rb.haml but on files that are not templates(models and controllers, for example). It seems that typical pre-processing is specific to the asset pipeline.
http://guides.rubyonrails.org/v3.2.17/asset_pipeline.html#preprocessing
Does anyone know where I could start in writing a pre-processor for code that is not part of the asset pipeline?
EDIT: I want to do something to the following effect:
Write Ruby code in a custom syntax.
Each file with the custom syntax has an extra extension to denote custom syntax. For example, the model page.rb would be page.rb.custom.
When Rails runs(or when Spring reloads the app), code that has a .custom file extension is preprocessed(or precompiled, rather) into pure Ruby and is executed.

How can I syntax check (not render) a Rails 3 ERB template file?

I'm trying to have a git pre-commit hook perform a syntax check on all Ruby code; there is one on GitHub at https://github.com/cypher/git-ruby-syntax-check.
It attempts to check .erb files by erb -x to translate them into Ruby code and then passes the output to ruby -c for syntax checking. Unfortunately, Rails 3 introduced a custom ERB parser that is incompatible with Ruby's standard ERB, and so the pre-commit hook is finding errors where there are none.
Is there some equivalent to erb -x that will output Ruby code from a Rails 3 ERB file?
I have not dug much into either of these but you might try rails-erb-check (Git project) or this blog entry. I agree with shingara but the Blog Post describes a situation where this is useful and I wonder if you are in a similar position:
Diaspora is pretty fluid right now. This means we are have some green
tests, some missing tests, and other tests that check intent (not
implementation). In an ideal world, I suppose test cases would cover
all of our bases...
Until then, I've added a new task to my fork, check_syntax:all. This
breaks down further to the subtasks check_syntax:erb,
check_syntax:haml, check_syntax:haml_ruby, check_syntax:sass, and
check_syntax:yaml.
If you get an "argument list too long error" for rails-erb-check , you can try rails-erb-lint which scans your current views folder.

Rails Generator: generate files based on already existing rails files

I wanted to make a generator that created files (and directories, etc...) based on already existing files in the app (for instance, the views or controllers). So if we had views set up like this
-app
-views
- layouts
- application.html.erb
- users
- index.html.erb
- show.html.erb
- etc ...
and I wanted to create files based on them I can do (with just ruby)
directories = Dir.entries("#{Rails.root}/app/views")
directories.each do |directory|
unless directory == "." or directory == ".."
files = Dir.entries("#{Rails.root}/app/views/#{directory}")
files.each do |file|
unless file == "." or file == ".."
text = File.read("#{Rails.root}/app/views/#{directory}/#{file}")
something #=> whatever else needs to go here to edit the file
something else #=> output_file.puts whatever
end
end
end
end
so this is basically what I would like to do with a generator so I can roll my code into a plugin and use it for other apps.
First question, how can I generate arbitrary files (with filenames based on existing filenames using the generator. Is it appropriate to cycle through the directories like I did above, grab the directory/file and generate files? Is there a way to do what I did using a simpler method (mine seems easily breakable).
Also, should I put all that read/format/write code inside the generator itself and just pass a string into the "initialize content" section of create_file or should I put it somewhere else. Or should I use the generator to create the bare files and populate it with an init script?
Is there a more rails type of way of populating generated files, or should I just shove all my formatting code inside the generator. If so, what is the appropriate way to approach this.
I am not sure if you want to know how generators are built in rails3 or not. The code you are showing is not very generator-like. In generators you can use all commands from Thor, which offers you a very powerful toolset of manipulating files, and injecting code (strings) into classes or files.
So I would most definitely fill your files inside a generator, because then it happens on user request, and the user can choose whether or not certain files need or can be overwritten or not.
Inside your gem, you will have a lib/generators folder, containing a templates folder, containing all files you might want to place inside the rails application.
From the Thor documentation, here is a nice example to construct files in a generator.
Hope this helps.
there's a simple API to use generators in Rails. here you can find a good guide:
http://guides.rubyonrails.org/generators.html
if you want to check some code:
https://github.com/coderloop/tamed_beast (I'm the author of its generators)
https://github.com/pilu/web-app-theme (another clean example)

How to create/use custom classes and helper in symfony 1.4?

What is the best way to put custom library or helper methods in symfony?
I am using doctrine with my project. One place I consider to put is under project_root/lib/vendor/MyClasses/
But if I want to create a class or helper function which will use some core symfony/doctrine methods and return a result then how to do that and where should I put it?
I want it to call from different modules to avoid code duplication.
As for the custom library part of the question, you might probably want to put your external library into the lib/vendor folder. Since symfony doesn't automatically load everything that's in there, you'll have to tell its autoloader to do so.
You can either extend your config/ProjectConfiguration.class.php (as described here) or (and this is the much simpler and cleaner way) you add them to your config/autoload.yml (you might have to create this one).
For the latter option, this is a great place to start looking at.
It seems to be a duplicate question.
As asked in symfony's 1.2 "Definitive Guide" docs :
Helper functions (regular PHP functions returning HTML code) should be saved in a file called FooBarHelper.php, where FooBar is the name of the helper group. Store the file in the apps/myapp/lib/helper/ directory (or in any helper/ directory created under one of the lib/ folders of your project) so it can be found automatically by the use_helper('FooBar') helper for inclusion.
So, if you want to create custom helper FooBar for foo() function, create file lib/helper/FooBarHelper.php :
function foo() {echo "foo!"; }
to use it in your template:
use_helper('FooBar')
....
foo(); //outs "foo!"

Resources