extjs / mvc

Ruby ExtJS Tools
http://www.extonrails.com
MIT License
118 stars 50 forks source link

Mapping support for extjs_store #1

Closed durchanek closed 15 years ago

durchanek commented 15 years ago

Just tried to use extjs-mvc gem and i found it really helpful. But I am missing mapping support for JsonStore which I think is needed to correctly handle associations. My idea is to alter extjs_fields call so it can handle nested columns, something like this:

extjs_fields :id, :foo, :bar => [:bar_field_1, :bar_field_2]

Another possibility which came to my mind is to have multiple configurations like this:

extjs_fields :simple_config, :include => :id, :name
extjs_fields :complex_config, :include => :id, :foo, :bar => [:bar_field_1, :bar_field_2]

This will not probably be backwards-compatible but can be useful when loading data depending on security setting/UI component/etc. I believe I can handle this on my own but I would like have this "approved" for inclusion before coding :-)

danielbeardsley commented 15 years ago

Wow, I'm using this gem, I've forked it, and have just added one of the features you suggested and plan to add the second. I'd like to be able to deal with multiple levels of hierarchy with associated columns.

I'm considering lots of different ways of specifying associated columns:

extjs_fields :field, :field2, :parent => [:field1, :field2]
extjs_fields :field, :field2, [:parent, :field1, :field2, [:grand_parent, :field1]]
extjs_fields :field, :field2, 'parent.field1', 'parent.field2'
extjs_fields :field, :field2, :parent => {:field1 => ''parent_field1', :field2 => 'parent_field2'}

Clone: http://github.com/danielbeardsley/mvc

durchanek commented 15 years ago

That's awesome :-) I was considering the same way which ActiveRecord uses now eg. for association eager loading - it would be developer-friendly for write them without much thinking. I think that your first example is perfect.

extjs commented 15 years ago

Yea, we need some smarter mechanism for associations.

Also, I'd like to be able to override fields when calling #to_record.

eg:

User.first.to_record(:username, :email)

extjs commented 15 years ago

I'm doing a big refactor. I'll soon see about the mapping property

durchanek commented 15 years ago

Great news, let me know if I can help.

extjs commented 15 years ago

Implemented mapping. Need to do some tests before I push new Gem version

extjs commented 15 years ago

Pushed version 0.2.7 with mapping support, with help from Daniel Beardsley.

durchanek commented 15 years ago

Hello, just tested new version and I was unable to get it work. It looks like some weird stuff in extjs_record(_fields): puts "1" + self.extjs_record_fields.inspect associations = self.extjs_associations puts "2" + self.extjs_record_fields.inspect # extjs_record_fields are gone here - only array of column names remains columns = self.extjs_columns_hash fields = fields.empty? ? self.extjs_record_fields : self.process_fields(_fields) pk = self.extjs_primary_key I was pretty much unable to find error here, because extjs_associations method does not touch extjs_record_fields at all.

extjs commented 15 years ago

What's your Model setup look like? Rails 2.3.4?

extjs commented 15 years ago

I've figured out a testing strategy and created a test-db with 2 models, User and Person. I pushed some simply Model tests to github.

durchanek commented 15 years ago

OK, I got it working, event mapping generates OK. But I had to comment out most of associations, my WORKING code looks like this

class Journalist < ActiveRecord::Base
  belongs_to :position, :class_name => 'JournalistPosition', :foreign_key => :position_id
  # has_and_belongs_to_many :magazines
  #  has_and_belongs_to_many :groups, :class_name => 'JournalistGroup', :join_table => :journalist_groups_journalists
  #  has_many :item_lends
  #  has_many :item_lend_wishes
  extjs_fields :name, :surname, :position => [:name]
end

I have included Extjs::Model to ActiveRecord::Base, so include line is missing here. When I uncomment has_and_belongs_to_many :magazines than error is back

extjs commented 15 years ago

I added a HABTM test in Github and it didn't fail. Have a look at test/model_test.rb I set up the test table and associations in test/test_helper.rb

durchanek commented 15 years ago

OK, I found it - it was because of my inclusion of Extjs::Model in initializer.