Class: Guilded::Rails::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/guilded/rails/helpers.rb

Overview

Common functionality that Rails Guilded components may need to use.

Class Method Summary collapse

Class Method Details

Helper method that generates a path from an item. If item is :home then this method will call the home_path method to generate a link



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/guilded/rails/helpers.rb', line 68

def self.generate_link( item )
  begin
    link_path = @controller.send( "#{item.to_s}_path" )
  rescue => e
    if e.is_a? NoMethodError
      link_path = ''
    else
      throw e
    end
  end
  link_path
end

.resolve_field_methods_and_titles(choices, ar_obj) ⇒ Object

Helper method that takes an array of choices, each being a string, symbol or hash, and resolves it into two arrays, one being an array of method names to get a fields value and the other being an array of titles to display the field as.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/guilded/rails/helpers.rb', line 85

def self.resolve_field_methods_and_titles( choices, ar_obj )
  titles = Array.new
  methods = Array.new

  choices.each do |choice|
    if choice.is_a? Hash
      if choice.to_a[0][1].is_a? Symbol
        titles.push( choice.to_a[0][1].to_s.humanize.titleize )
      else
        titles.push( choice.to_a[0][1] )
      end
      if choice.to_a[0][0].is_a? Symbol
        methods.push( choice.to_a[0][0].to_s )
      else
        methods.push( choice.to_a[0][0] )
      end
    else
      title = nil
      begin
        title = ar_obj.class.human_attribute_name( choice )
      rescue => ex
        title = choice.to_s.humanize.titleize if ex.is_a?( NoMethodError )
      end
      titles.push( title )
      methods.push( choice.to_s )
    end
  end

  return methods, titles
end

.resolve_rest_path_helpers(ar_obj_col_or_class, options = {}) ⇒ Object

Resolves the REST path helper names and arguments from a ActiveRecord object(s).



12
13
14
15
16
17
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/guilded/rails/helpers.rb', line 12

def self.resolve_rest_path_helpers( ar_obj_col_or_class, options={} )        
  if ar_obj_col_or_class.is_a?( Array )
    ar_obj = ar_obj_col_or_class[0]
  elsif ar_obj_col_or_class.is_a?( ActiveRecord::Base )
    ar_obj = ar_obj_col_or_class
  elsif ar_obj_col_or_class.is_a?( Class )
    ar_obj = ar_obj_col_or_class.new
  end
  
  plural_ar_type = ar_obj.class.to_s.tableize
  singular_ar_type = plural_ar_type.singularize
  polymorphic_as = options[:polymorphic_as]
  polymorphic_type = options[:polymorphic_type]
  plural_polymorphic_type = polymorphic_type ? polymorphic_type.to_s.tableize : nil
  singular_polymorphic_type = polymorphic_type ? plural_polymorphic_type.singularize : nil
  plural_derived_type = polymorphic_type.nil? ? plural_ar_type : plural_polymorphic_type
  singular_derived_type = polymorphic_type.nil? ? singular_ar_type : singular_polymorphic_type
  pre = ""
  shallow_pre = ""
  scoped_by = Array.new
  shallow_scoped_by = Array.new
  helpers = Hash.new
  
  if options[:namespace]
    pre << "#{options[:namespace].to_s}_"
    shallow_pre << "#{options[:namespace].to_s}_" 
  end
  
  if options[:scoped_by]
    scoped_by = options[:scoped_by].is_a?( Array ) ? options[:scoped_by] : Array.new << options[:scoped_by]
    scoped_by.each_with_index do |scoper, i|
      scoper_name = scoper.class.to_s.tableize.singularize
      shallow_pre << "#{scoper_name}_" unless options[:shallow] && i == scoped_by.size-1
      pre << "#{scoper_name}_"
    end
    shallow_scoped_by = scoped_by.clone
    shallow_scoped_by.pop if options[:shallow]
  end
  
  helpers[:index_rest_helper] = "#{pre}#{plural_derived_type}_path"
  helpers[:index_rest_args] = scoped_by
  helpers[:show_rest_helper] = "#{shallow_pre}#{singular_derived_type}_path"
  helpers[:show_rest_args] = shallow_scoped_by
  helpers[:new_rest_helper] = "new_#{pre}#{singular_derived_type}_path"
  helpers[:new_rest_args] = scoped_by
  helpers[:edit_rest_helper] = "edit_#{shallow_pre}#{singular_derived_type}_path"
  helpers[:edit_rest_args] = shallow_scoped_by
  helpers[:delete_rest_helper] = "delete_#{shallow_pre}#{singular_derived_type}_path"
  helpers[:delete_rest_args] = shallow_scoped_by
  
  return helpers
end