legacy_data
legacy_data copied to clipboard
ruby 1.9.1, rails 3... uninitialized constant
Note sure what I'm doing wrong here, but when I run the generator I get:
legacy_data-0.1.11/lib/legacy_data/table_class_name_mapper.rb:3:in `class:TableClassNameMapper': uninitialized constant LegacyData::TableClassNameMapper::Singleton (NameError)
To be honest I haven't used or looked at this in years.
I was able to reproduce your issue and made a fix so it now seems to work. Please take a look using
gem 'legacy_data', :git => 'https://github.com/alexrothenberg/legacy_data'
If it works I can release it to github as a new version.
Its 2016, and I have not found a better tool than what you wrote - generate models with validations and allow you to redefine your Model names. Thanks.
Note to others, although this Gem is Rails3, you can copy these model-files from a temporary Rails3 app into your more current Rails app. The "schema_to_scaffold" gem can help fill out the rest of the pieces, though if you modified the model-names, you will need to paste the new names into the output it generates.
Wow glad to know this is still providing value! If it'd be useful I can release a new gem version with the latest code just let me know.
A Rails4 version would be welcome. As this is among the first steps for a new app being ported to Rails, I just set up a "dummy" Rail3 app, run this, then copied the table-defs to the Rails4 app. AFAIK, there is no other gem providing similar value, other than one which only works in Rails2 (magic_model_generator).
Although the model-defs created do not work in Rails4 as created, but some work with sed and awk can fix them.
Fix: set_table_name -> self.table_name =
find ./app/models -type f -exec \
sed -i 's/set_table_name/self.table_name =/g' {} +
Fix: set_primary_key -> self.primary_key =
find ./app/models -type f -exec \
sed -i 's/set_primary_key/self.primary_key =/g' {} +
The "true, false" is not an array in Rails4, but no need to define this for each instance, so...
I Removed "def self.possible_values_for_" definition
tempfile=temp.tmp; sub="";
for filename in *; do \
awk -v sb="$sub" '/def self.possible_values_for_/,/end/ { if ( $0 ~ /end/ ) print sb; next } 1' "$filename" > "$tempfile" && mv "$tempfile" "$filename"; \
done
Replaced "possible_values_for_" reference in validation with [true, false]
find .../app/models -type f -exec \
sed -i 's/possible_values_for_.*,/[true, false],/g' {} +
Replaced leftover-trash in validation's ":message" line
find .../app/models -type f -exec \
sed -i "s/false], ')/false]/g" {} +
--Edit-- (I fixed the sed/awk stuff above, after further work with the Rails 4 app where I imported the models, for anyone who finds this later.)
Auto-finding relationships would also be great, though this would require significant extra work and possibly q/a steps to confirm assumptions.
To finish the job, I use the "schema_to_scaffold" gem, which provides the controller and view defs (but not model-validations), and 'skip' on overwriting the model.
@ZiaMM sounds like you have a good system in place. I'm not working on this gem anymore but would be happy to give you the commit bit and/or merge a pull.
Hi Alex, I'd like to fork and create a pull request at some point, time permitting. Hopefully, I can create a "complete process," using this gem and "schema_to_scaffold," that takes a person from a nothing but a DB from a non-Rails app to a Rails 4.x app.
As one generally needs to leave the existing app operational while developing the Rails version, the process must be repeatable - once to get your "starting point" for development, and again on the overnighter where the old app goes away, the DB is copied one last time, and the Rails app goes live.
Middle-steps include migrations for changing all the table-names, column-names, etc to "Rails-friendly" (after which I used your gem on a 2nd pass, to re-create new models with all the changed-naming). Next is building all the model-relationships (wish there was a drag-connect gui for this). Your gem gets one a big step in that direction by simply making the DB 'connect-able' to a Rails app - and I thank you for that..
For anyone else who is lead here searching for a modern answer, here is a first pass that at least gives me models that Rails 5 will load :) https://github.com/joshjordan/legacy_data
Thanks @joshjordan.