Is there a framework that allows me to define a model only once?

I just finished creating a couple of models and had to separately write all their attributes in 1) Rails ActiveRecord 2) Rails database migration and 3) Backbone.js model. I don't feel very DRY. I guess the first step in solving this problem would be to move to node.js where I can use CoffeeScript in the back and front (ideally reuse the same Backbone.js model), but what about the database schema? I understand that I can create a framework that generates SQL from model attributes, but before I get started, I was wondering if something like this already exists or is in development.

It would also be great if this framework could combine views and controllers so that I don't have apps / views, apps / controllers, apps / assets / javascripts / views, apps / assets / javascripts / routers, etc. all defining things like that in different places.

+3


source to share


3 answers


Check out Derby , a node.js active development framework that tries to unify views and controllers (and more):

[Derby] runs the same code on servers and browsers and synchronizes data automatically. Derby takes care of rendering, packaging, and model bindings out of the box. Since all functions are designed to work together, no code duplication and glue code is required.

I am building a small hobby app with Derby and it has been fun and interesting so far. The united new world has its own physics.

It should be noted that Derby is built on top of Racer , a "real-time model synchronization engine"; you might find it "heavy" if you were expecting something like light glue code that stitches Backbone.js, express, and some level of database together.




Edit : Other frameworks I haven't learned yet:

0


source


This is one of the goals of Wakanda .



0


source


For models, you can simply create a base class that inherits from it. You just need to call set_table_name

in derived classes. I got it over with. This has the advantage that you can customize the functionality in derived classes. Example:

app / models / widget.rb:

class Widget < ActiveRecord::Base
   def some_common_method
      # ...
   end
end

      

app / models / blue_widget.rb:

class BlueWidget < Widget
   set_table_name "blue_widgets"
   # special methods, if any
end

      

and etc.

For migration, simply define a function for the common table columns. For example:

def common_table_columns(t)
   t.integer :id
   t.stirng :some_string
   # etc
end

create_table(:table1) { |t| common_table_columns(t) }

[:table2,:table3,:table4].each do |name|
   create_table(name) { |t| common_table_columns(t) }
end

create_table :table5 do |t|
    common_table_columns(t)
    t.string :extra
end

      

Can't speak for Backbone.js (right now), but I'm 100% sure you can achieve the same elimination of duplicate code. Javascript is as dynamic as Ruby.

Having said all this, why do you have multiple identical tables? Are you using the right solution for the right problem?

0


source







All Articles