Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add :Rfactory command for test/factories/**/*.rb #85

Closed
sunaku opened this issue Apr 21, 2011 · 26 comments
Closed

add :Rfactory command for test/factories/**/*.rb #85

sunaku opened this issue Apr 21, 2011 · 26 comments

Comments

@sunaku
Copy link

sunaku commented Apr 21, 2011

Please add an :Rfactory command that gives quick access to test/factories/**/*.rb just like :Rmodel gives quick access to app/model/**/*.rb. Thanks!

@tpope
Copy link
Owner

tpope commented Apr 21, 2011

It's possible to define your own custom navigation commands in .vim/macros/rails.vim:

Rnavcommand factory   spec/factories test/factories  -suffix=_factory.rb -default=model()

I'm really conservative when it comes to supporting plugins in rails.vim. So far, I've caved on RSpec and Cucumber. :Rfactory isn't out of the question, but if I do it, I'm going to do it right, which means support for Machinist, Object Daddy, and Fabrication as well.

@sunaku
Copy link
Author

sunaku commented Apr 21, 2011

Awesome! Thanks for the snippet (I skimmed the code but didn't see how to define my own :R commands). I understand and agree with your reasoning. I only use FactoryGirl myself.

@tpope tpope closed this as completed Aug 27, 2011
@pjg
Copy link

pjg commented Mar 25, 2013

Since Rnavcommand has now been deprecated, how can I convert the above snippet for opening factories to use the new projections functionality? :)

@tpope
Copy link
Owner

tpope commented Mar 25, 2013

For the latest Factory Girl, which uses filenames like test/factories/users.rb, try this full featured projection:

{
  "test/factories/*.rb": {
    "command": "factory",
    "affinity": "collection",
    "alternate": "app/models/{singular}.rb",
    "related": "db/schema.rb#{}",
    "test": "test/models/{singular}_test.rb",
    "template": "FactoryGirl.define do\n  factory :{singular} do\n  end\nend",
    "keywords": "factory sequence"
  }
}

Repeat for spec, if desired. See the help for the the several different places you're allowed to define projections.

Update: changed %o to %i.
Update 2: changed % expansions to {} expansions.

@graywh
Copy link
Contributor

graywh commented Mar 25, 2013

And for the old *_factory.rb files you can do the same, but with 'affinity': 'model' and 'related': 'db/schema.rb#%p'.

@tpope
Copy link
Owner

tpope commented Mar 25, 2013

You also need to change the glob, and %o to %s.

On Mon, Mar 25, 2013 at 1:01 PM, Will Gray notifications@github.com wrote:

And for the old *_factory.rb files you can do the same, but with 'affinity':
'model' and 'related': 'db/schema.rb#%p'.


Reply to this email directly or view it on GitHubhttps://github.com//issues/85#issuecomment-15406044
.

@graywh
Copy link
Contributor

graywh commented Mar 25, 2013

Is there anything wrong with using an explicit %o or %p in place of %s when appropriate?

@tpope
Copy link
Owner

tpope commented Mar 25, 2013

echo rails#pluralize(rails#pluralize('user'))
userses

I might add %m and %c expansions at some point, but until then, no, don't do that.

I'm really annoyed that %s is original and %o is singular. :/

@graywh
Copy link
Contributor

graywh commented Mar 25, 2013

Good to know. I assumed %p and %o were "plural" and "singular", not "pluralized" and "singularized".

Edit: Guess I didn't pay enough attention when I looked at the docs.

tpope added a commit that referenced this issue Mar 26, 2013
@tpope
Copy link
Owner

tpope commented Mar 26, 2013

I am changing %o to %i because the o/s thing is just too confusing. Example updated.

@pjg
Copy link

pjg commented Apr 27, 2013

Thank you! This works as advertised :) (pjg/dotfiles@0c5e560)

@anhari
Copy link

anhari commented Dec 9, 2016

Awesome guys, thank you so much for this.

@kaka-ruto
Copy link

kaka-ruto commented Feb 13, 2020

Anyone else who comes here in future, Chris Toomey has a plugin for all of us -> https://github.com/christoomey/vim-rfactory

@tpope
Copy link
Owner

tpope commented Feb 13, 2020

:Efixtures opens factories now. Spread the word.

@kaka-ruto
Copy link

Thanks Tim! 😻

@Seybo
Copy link

Seybo commented May 24, 2021

any substitute to Efixtures already? Efixtures doesn't seem to work right now

@tpope
Copy link
Owner

tpope commented May 24, 2021

It works right now.

@Seybo
Copy link

Seybo commented May 25, 2021

it looks into spec/fixtures/ but my factories (and i suppose it's common) are in spec/factories. Maybe there is a simple way to handle it?

@tpope
Copy link
Owner

tpope commented May 25, 2021

It looks in both spec/fixtures and spec/factories.

@Seybo
Copy link

Seybo commented May 25, 2021

yeah, my bad. The problem in my case is that my project factories are called like modelname_factories.rb

@tpope
Copy link
Owner

tpope commented May 25, 2021

It's a bit clumsy but :Efixtures modelname<Tab> will still work.

@Seybo
Copy link

Seybo commented May 25, 2021

yeah. But can get better with a bit of a vimscript i guess :)
nmap <Leader>rb viw"sy:Efixtures <C-r>=tolower(substitute(substitute(@s, '\n', '', 'g'), '/', '\\/', 'g'))<cr>
My first attempt. Can't find a way to pass a tab in the end

@tpope
Copy link
Owner

tpope commented May 25, 2021

Tab complete is an interactive way to save typing a few characters. If you're scripting it you could just as well type out the _factories.

@Seybo
Copy link

Seybo commented May 25, 2021

yeah, but i've noticed that this binding works great for the vcr cassettes.

Like, I have a line like this in my spec:
vcr: { cassette_name: 'services/order_service/create' ...
i visually select the path part
i use this mapping: vmap <Leader>rb "sy:Efixtures <C-r>=tolower(substitute(substitute(@s, '\n', '', 'g'), '/', '\\/', 'g'))<cr>
and it works great, i only need to press tab and enter afterwards to open this cassete

so i would prefer to keep this binding universal

@Seybo
Copy link

Seybo commented May 25, 2021

gf doesn't work in this case because of cassettes .yml extension (it looks for .rb). Probably it can be fixed somehow and it would be a better solution

@tpope
Copy link
Owner

tpope commented May 25, 2021

This is wandering into the weeds so let me just conclude with a reminder that while FactoryBot will slurp up any .rb file in spec/factories or test/factories, the standard naming scheme - the one used by the generators - is modelnames.rb: plural with no suffix other than the file extension. It can optionally be configured to use a suffix like modelnames_factory.rb, which rails.vim doesn't support but could. It can only support a singular model name (modelname_factories.rb) with custom code. If that's really what you're doing, you're on your own.

gf doesn't work in this case because of cassettes .yml extension (it looks for .rb). Probably it can be fixed somehow and it would be a better solution

I'm gonna loosen that up a bit so that it doesn't force .rb on literal string values by default. That's not enough to make it work in this case, but it might if paired with :setlocal suffixesadd+=.yml.

tpope added a commit that referenced this issue May 25, 2021
algert072 added a commit to algert072/vim-rails that referenced this issue Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants