Class: Netzke::PropertyEditorExtras::HelperModel

Inherits:
Object
  • Object
show all
Defined in:
lib/netzke/property_editor_extras/helper_model.rb

Constant Summary collapse

DEFAULTS_FOR_FIELD =
{
  :Fixnum => {
    :xtype => :numberfield
  },
  :Boolean => {
    :xtype => :xcheckbox,
    :checked => true
  },
  :String => {
    :xtype => :textfield
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 95

def method_missing(method_name, *args)
  # Rails.logger.debug "!!! method_name: #{method_name.inspect}"
  method_name = method_name.to_s
  method_name_without_equal_sign = method_name.sub(/=$/, '')
  NetzkePreference.widget_name = self.class.widget.id_name

  if method_name =~ /=$/
    # current_value = NetzkePreference[method_name_without_equal_sign] # may be nil
    current_value = self.class.widget.flat_independent_config(method_name_without_equal_sign)
    
    begin
      new_value = ActiveSupport::JSON.decode(args.first) # TODO: provide feedback about this error
    rescue ActiveSupport::JSON::ParseError
      new_value = current_value
    end
    
    # default_value = self.class.widget.flat_default_config(method_name_without_equal_sign)
    initial_value = self.class.widget.flat_initial_config(method_name_without_equal_sign)

    new_value = nil if new_value == initial_value
    # Rails.logger.debug "!!! new_value: #{new_value.inspect}"
    NetzkePreference[method_name_without_equal_sign] = new_value
  else
    res = self.class.widget.flat_independent_config(method_name_without_equal_sign)
    res = ActiveSupport::JSON.encode(res) if res.is_a?(Array) || res.is_a?(Hash)
    res
  end
end

Class Method Details

.default_field_config(config) ⇒ Object

DRY out!



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 41

def self.default_field_config(config)
  type = config.delete(:type)

  common = {
    :field_label => config[:name].to_s.gsub('__', '_').humanize,
    :hidden      => config[:name] == :id
  }

  default = DEFAULTS_FOR_FIELD[type] || DEFAULTS_FOR_FIELD[:String] # fallback to plain textfield

  res = default.merge(common).merge(config)
end

.find_by_id(*args) ⇒ Object



54
55
56
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 54

def self.find_by_id(*args)
  self.new
end

.netzke_exposed_attributesObject



20
21
22
23
24
25
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 20

def self.netzke_exposed_attributes
  preferences = self.widget.flat_default_config
  # preferences = NetzkePreference.find_all_for_widget(widget.name)
  preferences.each { |p| p.reject!{ |k,v| k == :value}.merge!(:field_label => p[:name].to_s.gsub('__', "/").humanize) }
  preferences
end

.primary_keyObject



16
17
18
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 16

def self.primary_key
  "id"
end

.reflect_on_all_associationsObject



12
13
14
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 12

def self.reflect_on_all_associations
  []
end

.widgetObject



8
9
10
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 8

def self.widget
  @@widget ||= raise RuntimeError, "No widget specified for PropertyEditorExtras::HelperModel"
end

.widget=(w) ⇒ Object



4
5
6
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 4

def self.widget=(w)
  @@widget = w
end

Instance Method Details

#attributesObject

somewhat sofisticated code to convert all NetzkePreferences for current widget into a hash (“un-flatten”)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 80

def attributes
  prefs = NetzkePreference.find_all_for_widget(self.class.widget.id_name)
  res = {}
  prefs.each do |p|
    tmp_res = {}
    hsh_levels = p.name.split("__").map(&:to_sym)
    hsh_levels.each do |level_prefix|
      tmp_res[level_prefix] ||= level_prefix == hsh_levels.last ? p.normalized_value : {}
      res[level_prefix] = tmp_res[level_prefix] if level_prefix == hsh_levels.first
      tmp_res = tmp_res[level_prefix]
    end
  end
  res
end

#errorsObject



61
62
63
64
65
66
67
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 61

def errors
  a = Array.new
  def a.each_full
    []
  end
  a
end

#saveObject



58
59
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 58

def save
end

#to_array(columns, widget = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/netzke/property_editor_extras/helper_model.rb', line 69

def to_array(columns, widget = nil)
  res = []
  for c in columns
    method = c.is_a?(Symbol) ? c : c[:name]
    value = send(method)
    res << (value.is_a?(Array) || value.is_a?(Hash) ? value.to_json : value)
  end
  res
end