Module: Netzke::HasMany::Base::ClassMethods

Defined in:
lib/netzke/has_many/base.rb

Instance Method Summary collapse

Instance Method Details

#has_many(many, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/netzke/has_many/base.rb', line 9

def has_many many, options = {}
  grid_klass = options[:grid_klass].to_s if options[:grid_klass]
  endpoint_path = options[:endpoint] || :select_one
  endpoint_fun = endpoint_path.to_s.camelize
  endpoint_fun = endpoint_fun[0].downcase + endpoint_fun[1..-1]
  class_eval do
    action "edit_#{many}".to_sym

    endpoint endpoint_path do | params, this |
      component_session["selected_#{config["model"].underscore}_id"] = params["one_id"]
    end

    component "#{many}_window" do |c|
      c.klass = Window
      c.one_id = component_session["selected_#{config["model"].underscore}_id"]
      c.one_name = "#{config["model"].underscore}_id".to_sym
      c.grid_klass = grid_klass
      c.grid_model = many.to_s.singularize.camelize
    end

    js_configure do |c|
      c.send "on_edit_#{many}=", <<-JS
        function(){
          this.netzkeLoadComponent("#{many}_window", {callback: function(w){
            w.show();
          }});
        }
      JS

      c.init_component = <<-JS
        function(){
          Netzke.classes.#{self.to_s.gsub("::",".")}.superclass.initComponent.call(this);

          this.getSelectionModel().on('selectionchange', function(selModel){
            this.actions.edit#{many.to_s.camelize}.setDisabled(selModel.getCount() != 1);
            if(selModel.getCount() == 1) {
              one_id = selModel.store.getAt(selModel.getSelection()[0].index).get('id');
              this.#{endpoint_fun}({one_id: one_id})
            }
          }, this);
        }
      JS
    end

    define_method :default_bbar do |*params|
      ["edit_#{many}".to_sym, "-", *super(*params)]
    end

    define_method :default_context_menu do |*params|
      ["edit_#{many}".to_sym, "-", *super(*params)]
    end
  end
end