Class: ApplicationWidget

Inherits:
ApplicationController
  • Object
show all
Includes:
SingletonHelper
Defined in:
app/controllers/application_widget.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ ApplicationWidget

Returns a new instance of ApplicationWidget.



6
7
8
# File 'app/controllers/application_widget.rb', line 6

def initialize(request)
  self.request = request
end

Class Method Details

.description!(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'app/controllers/application_widget.rb', line 36

def self.description!(*args)
  if args.size == 1
    instance_variable_set(:@description_widget, args[0])
  elsif args.size == 2
    instance_variable_set("@description_#{args[0]}", args[1])
  end
end

.description?(attr = nil) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'app/controllers/application_widget.rb', line 31

def self.description?(attr=nil)
  attr='widget' if attr.nil?
  instance_variable_defined?("@description_#{attr}") ? instance_variable_get("@description_#{attr}") : ''
end

.name!(value) ⇒ Object



26
27
28
29
# File 'app/controllers/application_widget.rb', line 26

def self.name!(value)
  # define_singleton_method(:widget_name) {value}
  instance_variable_set(:@widget_name, value)
end

.name?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/controllers/application_widget.rb', line 22

def self.name?
  instance_variable_defined?(:@widget_name) ? instance_variable_get(:@widget_name) : ''
end

.widgetsObject



44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/application_widget.rb', line 44

def self.widgets
  # ApplicationWidget.descendants.map do |klass| #Não estava carregando no primeiro load...
  Dir["#{Rails.root}/app/widgets/*.rb"].map do |file_path|
    file_name = File.basename(file_path, ".rb")
    klass = Object.const_get file_name.classify
    actions = klass.instance_methods(false).map do |action|
      {name: action, description: klass.description?(action)}
    end
    {code: klass.to_s.tableize.sub('_widgets',''), name: klass.name?, description: klass.description?, actions: actions}
  end
end

Instance Method Details

#render_template(view_file = nil, *args) ⇒ Object



10
11
12
13
# File 'app/controllers/application_widget.rb', line 10

def render_template(view_file=nil, *args)
  args << {template: view_file}
  render_to_string *args
end

#view(view_file = nil, klass = nil) ⇒ Object



15
16
17
18
19
20
# File 'app/controllers/application_widget.rb', line 15

def view(view_file=nil, klass=nil)
  class_caller = klass || (caller[0].match(/(\b\w+)\.rb/)[1] rescue '')
  action_caller = view_file || (caller[0].match(/`(.*)'/)[1] rescue '')
  view_file = "widgets/#{class_caller.to_s.sub('_widget','')}/#{action_caller}" unless lookup_context.find_all(view_file).any?
  instance_variable_set(:@view_file, view_file)
end