Skip to content

Conversation

snarlysodboxer
Copy link

1, I like annotations after my models.
2, I frequently forget to add the '-p after' option when using this gem.
3, Simply running the command again adding the '-p after' option does not change the position since it detects that the model has already been annotated.
4, During writing this functionality I discovered you could simply pass the --force option, however there is nothing in the documentation about this, and it seems to me this functionality should be default, so I wrote the change anyway.

I understand if other people feel differently about moving the position of the annotations by default.

Much like the other code I see in annotate_models, to detect the need to move the position of the annotations, this code relies on the following two regular expressions to find the beginning and end of the string (model file):

/\A# == Schema Info/
/\n#\n\n\z/

@alexch
Copy link
Collaborator

alexch commented Apr 7, 2013

Auto-detecting position is a feature. Doesn't this remove that feature? Maybe instead we should just document better that -p defers to the current position unless --force is set.

On Apr 6, 2013, at 1:04 PM, David notifications@github.com wrote:

1, I like annotations after my models.
2, I frequently forget to add the '-p after' option when using this gem.
3, Simply running the command again adding the '-p after' option does not change the position since it detects that the model has already been annotated.
4, During writing this functionality I discovered you could simply pass the --force option, however there is nothing in the documentation about this, and it seems to me this functionality should be default, so I wrote the change anyway.

I understand if other people feel differently about moving the position of the annotations by default.

Much like the other code I see in annotate_models, to detect the need to move the position of the annotations, this code relies on the following two regular expressions to find the beginning and end of the string (model file):

/\A# == Schema Info/
/\n#\n\n\z/

You can merge this Pull Request by running

git pull https://github.com/snarlysodboxer/annotate_models master
Or view, comment on, or merge it at:

#119

Commit Summary

Automatically move position
File Changes

M lib/annotate/annotate_models.rb (9)
M spec/annotate/annotate_models_spec.rb (19)
Patch Links:

https://github.com/ctran/annotate_models/pull/119.patch
https://github.com/ctran/annotate_models/pull/119.diff

@namick
Copy link

namick commented Apr 8, 2013

If I understand this correctly, this does not remove auto-detection of the position, rather, it continues to auto-detect unless overridden by -p.

Currently -p is ignored if the schema has not changed. This seems to fix that.

+1 for me.

@snarlysodboxer
Copy link
Author

Correct me if I'm missing something here guys, but if you read the code you will find nothing that does any "detecting the position" in any way. (The two regex's I listed in this pull request would be this code base's first and only 'position-detection' code if they get accepted.) Let me explain for you:

As ctran/annotate_models stands right now:
Background: a model file is loaded into a variable that is a string.

  1. Annotate_models runs regular expressions on that string to detect any annotations it previously added, regardless of the position of those annotations. Those old annotations are put into a variable called old_columns. (Understand this to mean old_content, or old_annotations.)
  2. Annotate_models runs it's code that determines what the new_columns should be.
  3. It then compares old_columns to new_columns and:
  • If they're the same, it does nothing,
  • If they're different, it uses ruby's sub! command to replace old_columns with an empty string (regardless of old_column's position in the file), and then it continues on to again use sub! to replace an empty string at whatever-position-was-chosen-this-run with the content of new_columns.

Effectively, this means:
Given I have ten models and all of them are previously annotated with the -p after option.

  1. I run a migration that adds one column to one of my tables.
  2. I run annotate (forgetting to add the -p after option)
  3. I end up with 9 models annotated after, and 1 model annotated before.
    OK, no big deal:
  4. I realize my mistake, so I re-run it correctly annotate -p after
  5. No changes are made because the files have all already been annotated with the current schema. Doh!
  6. I then run annotate -p after --force and all ten of the models will be re-annotated, which is unnecessary and changes the mtime on all of the files, and that could lead to other annoying side effects.

Regardless of forgetting to add the -p after option the first time, doesn't it make sense that if I run the annotate command, then later run annotate -p after it should move the annotations to the after position? (And vise versa.) - This code provides that.

@ghost ghost assigned ctran Aug 15, 2013
@ctran
Copy link
Owner

ctran commented Feb 26, 2014

This now works as you expected in 2.6.2 by retaining the current position unless you pass "--force" option.

@ctran ctran closed this Feb 26, 2014
@ctran ctran modified the milestones: v2.6.2, v2.7.0 Nov 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants