erp_forms 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. data/GPL-3-LICENSE +674 -0
  2. data/README.rdoc +2 -0
  3. data/Rakefile +30 -0
  4. data/app/assets/javascripts/erp_forms/application.js +9 -0
  5. data/app/assets/stylesheets/erp_forms/application.css +7 -0
  6. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/base_controller.rb +47 -0
  7. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb +87 -0
  8. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/forms_controller.rb +79 -0
  9. data/app/controllers/erp_forms/erp_app/desktop/dynamic_forms/models_controller.rb +45 -0
  10. data/app/helpers/erp_forms/application_helper.rb +4 -0
  11. data/app/models/dynamic_datum.rb +65 -0
  12. data/app/models/dynamic_form.rb +191 -0
  13. data/app/models/dynamic_form_document.rb +29 -0
  14. data/app/models/dynamic_form_model.rb +42 -0
  15. data/app/views/layouts/application.html.erb +14 -0
  16. data/app/views/layouts/erp_forms/application.html.erb +14 -0
  17. data/app/widgets/dynamic_forms/base.rb +61 -0
  18. data/app/widgets/dynamic_forms/javascript/dynamic_forms.js +109 -0
  19. data/app/widgets/dynamic_forms/views/error.html.erb +4 -0
  20. data/app/widgets/dynamic_forms/views/index.html.erb +8 -0
  21. data/app/widgets/dynamic_forms/views/success.html.erb +4 -0
  22. data/config/routes.rb +6 -0
  23. data/db/data_migrations/20110608185830_create_default_dynamic_models_and_forms.rb +30 -0
  24. data/db/data_migrations/20110822170240_update_contact_form_validation.rb +22 -0
  25. data/db/data_migrations/20110828190913_create_desktop_app_dynamic_forms.rb +18 -0
  26. data/db/data_migrations/20110830170240_update_contact_form.rb +23 -0
  27. data/db/migrate/20110530193446_dynamic_forms.rb +82 -0
  28. data/lib/erp_forms/dynamic_attributes_patch.rb +10 -0
  29. data/lib/erp_forms/dynamic_form_field.rb +130 -0
  30. data/lib/erp_forms/dynamic_grid_column.rb +108 -0
  31. data/lib/erp_forms/engine.rb +22 -0
  32. data/lib/erp_forms/extensions/active_record/has_dynamic_data.rb +54 -0
  33. data/lib/erp_forms/extensions/active_record/has_dynamic_forms.rb +70 -0
  34. data/lib/erp_forms/extensions/extensions.rb +6 -0
  35. data/lib/erp_forms/extensions/railties/action_view.rb +20 -0
  36. data/lib/erp_forms/version.rb +3 -0
  37. data/lib/erp_forms.rb +11 -0
  38. data/lib/tasks/erp_forms_tasks.rake +4 -0
  39. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/center_region.js +23 -0
  40. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/dynamic_data_grid.js +81 -0
  41. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/form_builder.js +93 -0
  42. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/module.js +37 -0
  43. data/public/javascripts/erp_app/desktop/applications/dynamic_forms/west_region.js +468 -0
  44. data/public/javascripts/erp_app/shared/dynamic_forms/dynamic_forms_validation.js +4 -0
  45. data/spec/dummy/Rakefile +7 -0
  46. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  47. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  48. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  49. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  50. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  51. data/spec/dummy/config/application.rb +45 -0
  52. data/spec/dummy/config/boot.rb +10 -0
  53. data/spec/dummy/config/database.yml +8 -0
  54. data/spec/dummy/config/environment.rb +5 -0
  55. data/spec/dummy/config/environments/spec.rb +27 -0
  56. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  57. data/spec/dummy/config/initializers/inflections.rb +10 -0
  58. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  59. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  60. data/spec/dummy/config/initializers/session_store.rb +8 -0
  61. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  62. data/spec/dummy/config/locales/en.yml +5 -0
  63. data/spec/dummy/config/routes.rb +4 -0
  64. data/spec/dummy/config.ru +4 -0
  65. data/spec/dummy/public/404.html +26 -0
  66. data/spec/dummy/public/422.html +26 -0
  67. data/spec/dummy/public/500.html +26 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/script/rails +6 -0
  70. data/spec/models/dynamic_datum_spec.rb +9 -0
  71. data/spec/models/dynamic_form_document_spec.rb +11 -0
  72. data/spec/models/dynamic_form_model_spec.rb +11 -0
  73. data/spec/models/dynamic_form_spec.rb +11 -0
  74. data/spec/spec_helper.rb +60 -0
  75. metadata +183 -0
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ErpForms'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require "rspec/core/rake_task"
29
+ RSpec::Core::RakeTask.new(:spec)
30
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,47 @@
1
+ class ErpForms::ErpApp::Desktop::DynamicForms::BaseController < ::ErpApp::Desktop::BaseController
2
+ IGNORED_PARAMS = %w{action controller uuid widget_name widget_action dynamic_form_id dynamic_form_model_id model_name use_dynamic_form authenticity_token}
3
+
4
+ protected
5
+ def build_section_hash(website_section, website)
6
+ website_section_hash = {
7
+ :text => website_section.title,
8
+ :siteName => website.title,
9
+ :siteId => website.id,
10
+ :type => website_section.type,
11
+ :isSecured => !website_section.roles.empty?,
12
+ :isSection => true,
13
+ :inMenu => website_section.in_menu,
14
+ :hasLayout => !website_section.layout.blank?,
15
+ :id => "section_#{website_section.id}",
16
+ :url => "http://#{website.hosts.first.host}/#{website_section.permalink}"
17
+ }
18
+
19
+ if website_section.is_a?(Blog) || website_section.type == 'Blog'
20
+ website_section_hash[:isBlog] = true
21
+ website_section_hash[:iconCls] = 'icon-blog'
22
+ website_section_hash[:leaf] = true
23
+ else
24
+ unless website_section.children.empty?
25
+ website_section_hash[:leaf] = false
26
+ website_section_hash[:children] = []
27
+ website_section.positioned_children.each do |child|
28
+ website_section_hash[:children] << build_section_hash(child, website)
29
+ end
30
+ else
31
+ website_section_hash[:leaf] = true
32
+ end
33
+ website_section_hash[:iconCls] = website_section_hash[:isSecured] ? 'icon-document_lock' : 'icon-document'
34
+ end
35
+
36
+ website_section_hash
37
+ end
38
+
39
+ def page
40
+ offset = params[:start].to_f
41
+ offset > 0 ? (offset / params[:limit].to_f).to_i + 1 : 1
42
+ end
43
+
44
+ def per_page
45
+ params[:limit].nil? ? 20 : params[:limit].to_i
46
+ end
47
+ end
@@ -0,0 +1,87 @@
1
+ class ErpForms::ErpApp::Desktop::DynamicForms::DataController < ErpForms::ErpApp::Desktop::DynamicForms::BaseController
2
+
3
+ # setup dynamic data grid
4
+ def setup
5
+ form = DynamicForm.get_form(params[:model_name])
6
+ definition = form.definition_object
7
+
8
+ columns = []
9
+ definition.each do |field_hash|
10
+ field_hash[:width] = 120
11
+ columns << DynamicGridColumn.build_column(field_hash)
12
+ end
13
+
14
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated By", :name => 'updated_username', :xtype => 'textfield' })
15
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Created By", :name => 'created_username', :xtype => 'textfield' })
16
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Created At", :name => 'created_at', :xtype => 'datefield', :width => 75 })
17
+ columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated At", :name => 'updated_at', :xtype => 'datefield', :width => 75 })
18
+ columns << DynamicGridColumn.build_edit_column("Ext.getCmp('DynamicFormDataGridPanel').editRecord(rec,'#{params[:model_name]}');")
19
+ columns << DynamicGridColumn.build_delete_column("Ext.getCmp('DynamicFormDataGridPanel').deleteRecord(rec,'#{params[:model_name]}');")
20
+
21
+ definition << DynamicFormField.textfield({ :fieldLabel => "Updated By", :name => 'updated_username' })
22
+ definition << DynamicFormField.textfield({ :fieldLabel => "Created By", :name => 'created_username' })
23
+ definition << DynamicFormField.datefield({ :fieldLabel => "Created At", :name => 'created_at' })
24
+ definition << DynamicFormField.datefield({ :fieldLabel => "Updated At", :name => 'updated_at' })
25
+ definition << DynamicFormField.hidden({ :fieldLabel => "ID", :name => 'id' })
26
+
27
+ render :inline => "{
28
+ \"success\": true,
29
+ \"columns\": [#{columns.join(',')}],
30
+ \"fields\": #{definition.to_json}
31
+ }"
32
+ end
33
+
34
+ # get dynamic data records
35
+ def index
36
+ sort = params[:sort] || 'created_at'
37
+ dir = params[:dir] || 'DESC'
38
+
39
+ myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
40
+
41
+ dynamic_records = myDynamicObject.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}")
42
+
43
+ wi = []
44
+ dynamic_records.each do |i|
45
+ wihash = i.data.dynamic_attributes_without_prefix
46
+ # puts i.data.created_by.inspect
47
+ wihash[:id] = i.id
48
+ wihash[:created_username] = i.data.created_by.nil? ? '' : i.data.created_by.username
49
+ wihash[:updated_username] = i.data.updated_by.nil? ? '' : i.data.updated_by.username
50
+ wihash[:created_at] = i.data.created_at
51
+ wihash[:updated_at] = i.data.updated_at
52
+ wi << wihash
53
+ end
54
+
55
+ render :inline => "{ total:#{dynamic_records.total_entries}, data:#{wi.to_json} }"
56
+ end
57
+
58
+ # create a dynamic data record
59
+ def create
60
+ @myDynamicObject = DynamicFormModel.get_instance(params[:model_name])
61
+ puts current_user.inspect
62
+ params[:created_by] = current_user unless current_user.nil?
63
+ params[:created_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
64
+ @myDynamicObject = DynamicFormModel.save_all_attributes(@myDynamicObject, params, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
65
+
66
+ render :json => @myDynamicObject ? {:success => true} : {:success => false}
67
+ end
68
+
69
+ # update a dynamic data record
70
+ def update
71
+ @myDynamicObject = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
72
+
73
+ params[:updated_by] = current_user unless current_user.nil?
74
+ params[:updated_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
75
+ @myDynamicObject = DynamicFormModel.save_all_attributes(@myDynamicObject, params, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
76
+
77
+ render :json => @myDynamicObject ? {:success => true} : {:success => false}
78
+ end
79
+
80
+ # delete a dynamic data record
81
+ def delete
82
+ @myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
83
+ @myDynamicObject.destroy(params[:id])
84
+ render :json => {:success => true}
85
+ end
86
+
87
+ end
@@ -0,0 +1,79 @@
1
+ class ErpForms::ErpApp::Desktop::DynamicForms::FormsController < ErpForms::ErpApp::Desktop::DynamicForms::BaseController
2
+
3
+ # get tree of dynamic models and forms
4
+ def get_tree
5
+ models = params[:node] == "root" ? DynamicFormModel.all(:order => :model_name) : DynamicFormModel.find_all_by_id(id)
6
+ tree = []
7
+
8
+ models.each do |form_model|
9
+ # DynamicFormDocument only exists for the purpose of Extending
10
+ next if form_model.model_name == 'DynamicFormDocument'
11
+
12
+ model_hash = {
13
+ :text => form_model.model_name,
14
+ :iconCls => 'icon-data',
15
+ :id => "form_model_#{form_model.id}",
16
+ :modelId => form_model.id,
17
+ :isModel => true,
18
+ :isForm => false,
19
+ :leaf => false,
20
+ :children => []
21
+ }
22
+
23
+ #handle sections
24
+ form_model.dynamic_forms.each do |form|
25
+ form_hash = {
26
+ :text => form.description,
27
+ :iconCls => (form.default ? 'icon-document_ok' : 'icon-document'),
28
+ :id => "form_#{form.id}",
29
+ :formId => form.id,
30
+ :isModel => false,
31
+ :isDefault => form.default,
32
+ :modelName => form_model.model_name,
33
+ :isForm => true,
34
+ :leaf => true
35
+ }
36
+
37
+ model_hash[:children] << form_hash
38
+ end
39
+
40
+ #added website to main tree
41
+ tree << model_hash
42
+ end
43
+
44
+ render :json => tree
45
+ end
46
+
47
+ # get a single form definition
48
+ def get_definition
49
+ form = DynamicForm.get_form(params[:model_name], params[:internal_identifier])
50
+
51
+ render :json => form.definition
52
+ end
53
+
54
+ # get a single form
55
+ def get
56
+ form = DynamicForm.find(params[:id]).to_extjs_formpanel(
57
+ { :url => "/erp_forms/erp_app/desktop/dynamic_forms/data/#{params[:form_action]}",
58
+ :record_id => params[:id]
59
+ })
60
+
61
+ render :json => form
62
+ end
63
+
64
+ # delete dynamic form
65
+ def delete
66
+
67
+ end
68
+
69
+ # update dynamic form
70
+ def update
71
+
72
+ end
73
+
74
+ # create dynamic form
75
+ def create
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,45 @@
1
+ class ErpForms::ErpApp::Desktop::DynamicForms::ModelsController < ErpForms::ErpApp::Desktop::DynamicForms::BaseController
2
+
3
+ # get dynamic form models
4
+ # used in dynamic forms widget combo box
5
+ def index
6
+ models = []
7
+ dynamic_form_models = DynamicFormModel.where("model_name != 'DynamicFormDocument'").order('model_name ASC')
8
+ dynamic_form_models.each do |m|
9
+ model_hash = {
10
+ :id => m.id,
11
+ :model_name => m.model_name,
12
+ }
13
+
14
+ models << model_hash
15
+ end
16
+
17
+ render :json => models
18
+ end
19
+
20
+ # set default form for this model
21
+ def set_default_form
22
+ myDynamicObject = DynamicFormModel.get_constant(params[:model_name])
23
+ myDynamicObject.set_default(params[:id])
24
+
25
+ render :json => {success => true}
26
+ end
27
+
28
+ # delete a dynamic form model
29
+ def delete
30
+ DynamicFormModel.destroy(params[:id])
31
+
32
+ render :json => {success => true}
33
+ end
34
+
35
+ # create a dynamic form model
36
+ def create
37
+ model_name = params[:model_name]
38
+ DynamicFormModel.create({
39
+ :model_name => model_name
40
+ })
41
+
42
+ render :json => {success => true}
43
+ end
44
+
45
+ end
@@ -0,0 +1,4 @@
1
+ module ErpForms
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,65 @@
1
+ class DynamicDatum < ActiveRecord::Base
2
+ DYNAMIC_ATTRIBUTE_PREFIX = 'dyn_'
3
+
4
+ has_dynamic_attributes :dynamic_attribute_prefix => DYNAMIC_ATTRIBUTE_PREFIX, :destroy_dynamic_attribute_for_nil => false
5
+
6
+ belongs_to :reference, :polymorphic => true
7
+ belongs_to :created_with_form, :class_name => "DynamicForm"
8
+ belongs_to :updated_with_form, :class_name => "DynamicForm"
9
+ belongs_to :created_by, :class_name => "User"
10
+ belongs_to :updated_by, :class_name => "User"
11
+
12
+ def dynamic_attributes_without_prefix
13
+ attrs = {}
14
+ self.dynamic_attributes.each do |k,v|
15
+ attrs[k[DYNAMIC_ATTRIBUTE_PREFIX.length..(k.length)]] = v
16
+ end
17
+
18
+ attrs
19
+ end
20
+
21
+ =begin
22
+ # this was used for sorting dynamic sttributes under Ruby 1.8.7 which did not have ordered hashes
23
+ # it is now obsolete under Ruby 1.9
24
+ # leave it here for reference for the time being in case we find that dynamic attributes are stored in the wrong order in the DB
25
+ # using ordered hashes assumes that dynamic attributes are stored in order in the database
26
+ def sorted_dynamic_attributes(with_prefix=true)
27
+ if !self.updated_with_form.nil?
28
+ form = self.updated_with_form
29
+ elsif !self.created_with_form.nil?
30
+ form = self.created_with_form
31
+ else
32
+ form = nil
33
+ end
34
+
35
+ unless form.nil?
36
+ if with_prefix
37
+ keys = form.definition_object.collect{|f| DYNAMIC_ATTRIBUTE_PREFIX + f[:name]}
38
+ else
39
+ keys = form.definition_object.collect{|f| f[:name]}
40
+ end
41
+
42
+ sorted = []
43
+ keys.each do |key|
44
+ attribute = {}
45
+ if with_prefix
46
+ attribute[key] = self.dynamic_attributes[key]
47
+ else
48
+ attribute[key] = self.dynamic_attributes_without_prefix[key]
49
+ end
50
+
51
+ sorted << attribute
52
+ end
53
+
54
+ return sorted
55
+ else
56
+ if with_prefix
57
+ return self.dynamic_attributes
58
+ else
59
+ return self.dynamic_attributes_without_prefix
60
+ end
61
+ end
62
+ end
63
+ =end
64
+
65
+ end
@@ -0,0 +1,191 @@
1
+ class DynamicForm < ActiveRecord::Base
2
+ belongs_to :dynamic_form_model
3
+
4
+ validates_uniqueness_of :internal_identifier, :scope => :model_name
5
+
6
+ def self.class_exists?(class_name)
7
+ result = nil
8
+ begin
9
+ klass = Module.const_get(class_name)
10
+ result = klass.is_a?(Class) ? ((klass.superclass == ActiveRecord::Base or klass.superclass == DynamicModel) ? true : nil) : nil
11
+ rescue NameError
12
+ result = nil
13
+ end
14
+ result
15
+ end
16
+
17
+ def self.get_form(klass_name, internal_identifier='')
18
+ result = nil
19
+ if internal_identifier and internal_identifier != ''
20
+ result = DynamicForm.find_by_model_name_and_internal_identifier(klass_name, internal_identifier)
21
+ else
22
+ result = DynamicForm.find_by_model_name_and_default(klass_name, true)
23
+ end
24
+ result
25
+ end
26
+
27
+ # parse JSON definition into a ruby object
28
+ # returns an array of hashes
29
+ def definition_object
30
+ o = JSON.parse(self.definition)
31
+ o.map do |i|
32
+ i = i.symbolize_keys
33
+ end
34
+ end
35
+
36
+ def definition_with_validation
37
+ add_validation(self.definition_object)
38
+ end
39
+
40
+ def add_validation(def_object)
41
+ def_object.each do |item|
42
+ if item[:validator_function] and item[:validator_function] != ""
43
+ item[:validator] = NonEscapeJsonString.new("function(v){ regex = this.initialConfig.validation_regex; return #{item[:validator_function]}; }")
44
+ elsif item[:validation_regex] and item[:validation_regex] != ""
45
+ item[:validator] = NonEscapeJsonString.new("function(v){ return validate_regex(v, this.initialConfig.validation_regex); }")
46
+ end
47
+ end
48
+
49
+ def_object
50
+ end
51
+
52
+ # check field against form definition to see if field still exists
53
+ # returns true if field does not exist
54
+ def deprecated_field?(field_name)
55
+ result = true
56
+ definition_object.each do |field|
57
+ result = false if field[:name] == field_name.to_s
58
+ end
59
+
60
+ result
61
+ end
62
+
63
+ def self.concat_fields_to_build_definition(array_of_fields)
64
+ array_of_fields.to_json
65
+ end
66
+
67
+ def to_extjs_formpanel(options={})
68
+ form_hash = {
69
+ :xtype => 'form',
70
+ :id => 'dynamic_form_panel',
71
+ :url => options[:url],
72
+ :title => self.description,
73
+ :frame => true,
74
+ :bodyStyle => 'padding: 5px 5px 0;'
75
+ }
76
+
77
+ form_hash[:width] = options[:width] if options[:width]
78
+ form_hash[:baseParams] = {}
79
+ form_hash[:baseParams][:id] = options[:record_id] if options[:record_id]
80
+ form_hash[:baseParams][:dynamic_form_id] = self.id
81
+ form_hash[:baseParams][:dynamic_form_model_id] = self.dynamic_form_model_id
82
+ form_hash[:baseParams][:model_name] = self.model_name
83
+ form_hash[:listeners] = {:afterrender => NonEscapeJsonString.new("function(form) {Ext.getCmp('dynamic_form_panel').getComponent(0).focus(false);}")}
84
+ form_hash[:items] = definition_with_validation
85
+ form_hash[:buttons] = []
86
+ form_hash[:buttons][0] = {
87
+ :text => 'Submit',
88
+ :listeners => NonEscapeJsonString.new("{
89
+ \"click\":function(button){
90
+ var formPanel = Ext.getCmp('dynamic_form_panel');
91
+ formPanel.getForm().submit({
92
+ reset:true,
93
+ success:function(form, action){
94
+ Ext.getCmp('dynamic_form_panel').findParentByType('window').close();
95
+ if (Ext.getCmp('DynamicFormDataGridPanel')){
96
+ Ext.getCmp('DynamicFormDataGridPanel').query('shared_dynamiceditablegrid')[0].store.load();
97
+ }
98
+ },
99
+ failure:function(form, action){
100
+ Ext.Msg.alert(action.response.responseText);
101
+ }
102
+ });
103
+ }
104
+ }")
105
+ }
106
+ form_hash[:buttons][1] = {
107
+ :text => 'Cancel',
108
+ :listeners => NonEscapeJsonString.new("{
109
+ \"click\":function(button){
110
+ Ext.getCmp('dynamic_form_panel').findParentByType('window').close();
111
+ }
112
+ }")
113
+ }
114
+
115
+ form_hash
116
+ end
117
+
118
+ # convert form definition to ExtJS form
119
+ # definition is essentially the formpanel items
120
+ #
121
+ # options hash:
122
+ # :url => pass in URL for form to post to
123
+ # :widget_result_id =>
124
+ # :width =>
125
+ def to_extjs_widget(options={})
126
+ options[:width] = "'auto'" if options[:width].nil?
127
+
128
+ #NOTE: The random nbsp; forces IE to eval this javascript!
129
+ javascript = "&nbsp<script type=\"text/javascript\">
130
+ Ext.onReady(function(){
131
+ Ext.QuickTips.init();
132
+
133
+ var dynamic_form = Ext.create('Ext.form.Panel',{
134
+ id: 'dynamic_form_panel',
135
+ url:'#{options[:url]}',
136
+ title: '#{self.description}',"
137
+
138
+ javascript += "\"width\": #{options[:width]}," if options[:width]
139
+
140
+ javascript += "frame: true,
141
+ bodyStyle:'padding: 5px 5px 0;',
142
+ renderTo: 'dynamic_form_target',
143
+ baseParams: {
144
+ dynamic_form_id: #{self.id},
145
+ dynamic_form_model_id: #{self.dynamic_form_model_id},
146
+ model_name: '#{self.model_name}'
147
+ },
148
+ items: #{definition_with_validation},
149
+ listeners: {
150
+ afterrender: function(form) {
151
+ Ext.getCmp('dynamic_form_panel').getComponent(0).focus(false);
152
+ }
153
+ },
154
+ buttons: [{
155
+ text: 'Submit',
156
+ listeners:{
157
+ 'click':function(button){
158
+
159
+ var formPanel = Ext.getCmp('dynamic_form_panel');
160
+ formPanel.getForm().submit({
161
+ reset:true,
162
+ success:function(form, action){
163
+ json_hash = Ext.decode(action.response.responseText);
164
+ Ext.get('#{options[:widget_result_id]}').dom.innerHTML = json_hash.response;
165
+ var scriptTags = Ext.get('#{options[:widget_result_id]}').dom.getElementsByTagName('script');
166
+ Ext.each(scriptTags, function(scriptTag){
167
+ eval(scriptTag.text);
168
+ });
169
+ },
170
+ failure:function(form, action){
171
+ if (action.response){
172
+ json_hash = Ext.decode(action.response.responseText);
173
+ Ext.get('#{options[:widget_result_id]}').dom.innerHTML = json_hash.response;
174
+ }
175
+ }
176
+ });
177
+ }
178
+ }
179
+
180
+ },{
181
+ text: 'Cancel'
182
+ }]
183
+ });
184
+ });
185
+
186
+ </script>"
187
+ #logger.info javascript
188
+ javascript
189
+ end
190
+
191
+ end
@@ -0,0 +1,29 @@
1
+ class DynamicFormDocument < ActiveRecord::Base
2
+ belongs_to :dynamic_form_model
3
+
4
+ has_dynamic_forms
5
+ has_dynamic_data
6
+
7
+ # declare a subclass
8
+ # pass in name of subclass
9
+ def self.declare(klass_name)
10
+ Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name) and !Rails.configuration.cache_classes
11
+ Object.const_set(klass_name, Class.new(DynamicFormDocument)) unless Object.const_defined?(klass_name)
12
+ end
13
+
14
+ def send_email
15
+ WebsiteInquiryMailer.deliver_inquiry(self)
16
+ end
17
+
18
+ def self.class_exists?(class_name)
19
+ result = nil
20
+ begin
21
+ klass = Module.const_get(class_name)
22
+ result = klass.is_a?(Class) ? ((klass.superclass == ActiveRecord::Base or klass.superclass == DynamicModel) ? true : nil) : nil
23
+ rescue NameError
24
+ result = nil
25
+ end
26
+ result
27
+ end
28
+
29
+ end
@@ -0,0 +1,42 @@
1
+ class DynamicFormModel < ActiveRecord::Base
2
+ has_many :dynamic_form_documents
3
+ has_many :dynamic_forms, :dependent => :destroy
4
+
5
+ def self.get_constant(klass_name)
6
+ result = nil
7
+ begin
8
+ result = klass_name.constantize
9
+ rescue
10
+ DynamicFormDocument.declare(klass_name)
11
+ result = klass_name.constantize
12
+ end
13
+ result
14
+ end
15
+
16
+ def self.get_instance(klass_name)
17
+ DynamicFormModel.get_constant(klass_name).new
18
+ end
19
+
20
+ # handles both static and dynamic attributes
21
+ def self.save_all_attributes(dynamicObject, params, ignored_params=[])
22
+
23
+ params.each do |k,v|
24
+ unless ignored_params.include?(k.to_s) or k == '' or k == '_'
25
+ if dynamicObject.attributes.include?(k)
26
+ dynamicObject.send(k + '=', v)
27
+ else
28
+ if ['created_by','updated_by','created_at','updated_at','created_with_form_id','updated_with_form_id'].include?(k)
29
+ key = k + '='
30
+ else
31
+ key = DynamicDatum::DYNAMIC_ATTRIBUTE_PREFIX + k + '='
32
+ end
33
+
34
+ dynamicObject.data.send(key, v)
35
+ end
36
+ end
37
+ end
38
+
39
+ (dynamicObject.valid? and dynamicObject.save) ? dynamicObject : nil
40
+ end
41
+
42
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>ErpForms</title>
5
+ <%= stylesheet_link_tag "erp_forms/application" %>
6
+ <%= javascript_include_tag "erp_forms/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>ErpForms</title>
5
+ <%= stylesheet_link_tag "erp_forms/application" %>
6
+ <%= javascript_include_tag "erp_forms/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>