Class: ErpApp::Widgets::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_app/widgets/loader.rb

Class Method Summary collapse

Class Method Details

.load_compass_ae_widgets(config, engine) ⇒ Object



5
6
7
8
9
10
# File 'lib/erp_app/widgets/loader.rb', line 5

def load_compass_ae_widgets(config, engine)
  ErpBaseErpSvcs.setup_compass_ae_callback(config, engine) do |engine|
    ::ErpApp::Widgets::Loader.require_widgets_and_helpers(engine.root)
    ::ErpApp::Widgets::Loader.load_widget_extensions(engine)
  end
end

.load_root_widgets(config) ⇒ Object



12
13
14
15
16
# File 'lib/erp_app/widgets/loader.rb', line 12

def load_root_widgets(config)
   ErpBaseErpSvcs.setup_compass_ae_callback(config, nil) do |engine|
     ::ErpApp::Widgets::Loader.require_widgets_and_helpers(Rails.root.to_s)
   end
end

.load_widget_extensions(engine) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/erp_app/widgets/loader.rb', line 49

def load_widget_extensions(engine)
  widgets_path = File.join(engine.root,"lib",engine.railtie_name,"extensions/widgets")
  widgets = File.directory?(widgets_path) ? Dir.entries(widgets_path) : []
  widgets.delete_if{|name| name =~ /^\./}

  widgets.each do |widget_name|
    widget_hash = Rails.application.config.erp_app.widgets.find{|item| item[:name] == widget_name}
    #load any extensions to existing widgets
    Dir.glob(File.join(widgets_path,widget_name,"*.rb")).each do |file|
      load file
    end

    #add any additional view paths to widgets
    if File.directory?(File.join(widgets_path,widget_name,'views'))
      view_path = File.join(widgets_path,widget_name,'views')
      widget_hash[:view_paths].push(view_path)
      #get all view files for theme generation
      get_widget_view_files(widget_hash, view_path)
    end

    #overwrite with any extensions in rails root
    load_root_widget_extensions(widget_hash)
  end

end

.require_widgets_and_helpers(path) ⇒ Object



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
# File 'lib/erp_app/widgets/loader.rb', line 18

def require_widgets_and_helpers(path)
  widgets = []
  widgets_path = File.join(path,"/app/widgets/")
  widgets = Dir.entries(widgets_path) if File.exists? widgets_path
  widgets.delete_if{|name| name =~ /^\./}
  widgets.each do |widget_name|
    widget_hash = Rails.application.config.erp_app.widgets.find{|item| item[:name] == widget_name}
    if widget_hash.nil?
      widget_hash = {
        :name => widget_name,
        :path => File.join(widgets_path,widget_name),
        :view_paths => [File.join(widgets_path,widget_name,'views')],
        :view_files => []
      }
      Rails.application.config.erp_app.widgets << widget_hash
    end
    
    load File.join(widget_hash[:path],'base.rb')
    #load helpers
    if File.exists? File.join(widget_hash[:path],'helpers')
      load_widget_view_helpers File.join(widget_hash[:path],'helpers','view') if File.directory? File.join(widget_hash[:path],'helpers','view')
      load_widget_controller_helpers(File.join(widget_hash[:path],'helpers','controller'),widget_hash) if File.directory? File.join(widget_hash[:path],'helpers','controller')
    end
    #get all view files for theme generation
    get_widget_view_files(widget_hash, widget_hash[:view_paths].first)

    #load root widget extensions that should override this widget
    load_root_widget_extensions(widget_hash)
  end
end