Class: Codus::ViewHelpers::YojsHelper::YojsCallGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, action, options = {}) ⇒ YojsCallGenerator

Returns a new instance of YojsCallGenerator.



5
6
7
8
9
10
11
# File 'lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb', line 5

def initialize(controller, action, options = {})
  @options = merge_options_with_defaults(options)
  @parent_namespace = "#{@options[:app_name]}.#{controller.parameterize('.')}"
  @last_namespace = action.parameterize(".")
  @fullnamespace = "#{@parent_namespace}.#{@last_namespace}"
  
end

Instance Method Details

#append_onload_method_to_namespaces(namespaces = []) ⇒ Object



48
49
50
51
52
53
# File 'lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb', line 48

def append_onload_method_to_namespaces(namespaces = [])
  return namespaces if @options[:onload_method_name].nil? || @options[:onload_method_name] == ""
  namespaces.map do |namespace_to_call_onload|
    "#{namespace_to_call_onload}.#{@options[:onload_method_name]}"
  end
end

#generate_conditional_function_calling_for_namespaces(namespaces = []) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb', line 55

def generate_conditional_function_calling_for_namespaces(namespaces = [])
  namespaces.map do |call_definition| 
    "
      if (yojs.isDefined(\"#{call_definition}\")) {
        yojs.call(\"#{call_definition}\");
      }
    "
  end
end

#generate_yojs_callsObject



26
27
28
29
30
31
# File 'lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb', line 26

def generate_yojs_calls
  namespaces_to_call_onload = get_all_namespaces_from_full_namespace
  onload_calls_definitions = append_onload_method_to_namespaces(namespaces_to_call_onload)
  conditional_callings = generate_conditional_function_calling_for_namespaces(onload_calls_definitions)
  conditional_callings.join("\n").html_safe
end

#get_all_namespaces_from_full_namespaceObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb', line 33

def get_all_namespaces_from_full_namespace
  namespaces = []
  @fullnamespace.split(".").each do |actual_namespace| 
    if namespaces.empty?
      namespaces << actual_namespace
    else
      namespaces << ("#{namespaces.last}.#{actual_namespace}")
    end
  end
  if @options[:method_names_mapper].has_key?(@fullnamespace.split(".").last.to_sym)
    namespaces << ("#{@parent_namespace}.#{@options[:method_names_mapper][@last_namespace.to_sym]}")
  end
  namespaces
end

#merge_options_with_defaults(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/codus/view_helpers/yojs_helper/yojs_call_generator.rb', line 13

def merge_options_with_defaults(options)
  default_options = { :app_name => "", 
    :onload_method_name => "",
    :method_names_mapper => {
      :create => :new,
      :update => :edit
    }
  }
  mapping_options = options.delete(:method_names_mapper)
  default_options[:method_names_mapper].merge!(mapping_options) unless mapping_options.nil? 
  default_options.merge(options)
end