Class: Cambium::AdminPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/cambium/admin_presenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(action_view) ⇒ AdminPresenter

Returns a new instance of AdminPresenter.



6
7
8
# File 'app/presenters/cambium/admin_presenter.rb', line 6

def initialize(action_view)
  @view = action_view
end

Instance Method Details

#routes(object) ⇒ Object

Resolve the routes for the given controller



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/presenters/cambium/admin_presenter.rb', line 49

def routes(object)
  begin
    n = @view.cambium
    @view.cambium.send("admin_#{@view.controller_name.pluralize}_path")
  rescue
    n = @view.main_app
  end
  r = {
    :index => n.send("admin_#{@view.controller_name.pluralize}_path"),
    :new => n.send("new_admin_#{@view.controller_name.singularize}_path")
  }
  if !object.nil? && object.id.present?
    r[:edit] = n.send(
      "edit_admin_#{@view.controller_name.singularize}_path",
      object
    )
    r[:show] = n.send(
      "admin_#{@view.controller_name.singularize}_path",
      object
    )
    r[:delete] = r[:show]
  end
  r.to_ostruct
end

Resolves the values from config/admin/sidebar.yml and puts them in an array



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
# File 'app/presenters/cambium/admin_presenter.rb', line 13

def sidebar
  @sidebar ||= begin
    sidebar = []
    load_config('sidebar').each { |k, attrs| sidebar << attrs.to_ostruct }
    sidebar.each do |attrs|
      # resolve route
      path = "#{attrs.route.split('.').last}_path"
      if attrs.route.split('.').first == 'cambium'
        attrs.route = @view.cambium.send(path)
      else
        attrs.route = @view.main_app.send(path)
      end
      # figure out if it's active
      attrs.active = false
      if(
        (
          attrs.controllers &&
          attrs.controllers.include?(@view.controller_name)
        ) || @view.request.path == attrs.route
      )
        attrs.active = true
      end
    end
    sidebar
  end
end

#to_csv(collection) ⇒ Object

Export a collection of objects to csv, based on the configuration



77
78
79
80
81
82
83
84
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
115
116
117
118
# File 'app/presenters/cambium/admin_presenter.rb', line 77

def to_csv(collection)
  config = view(@view.controller_name).export
  output = CSV.generate do |csv|
    # Grab headings and methods
    headings = []
    methods = []
    config.columns.each_pair do |key, data|
      if data.label.nil?
        headings << key.to_s.titleize
      else
        headings << data.label
      end
      if data.output.nil?
        methods << key.to_s
      else
        methods << data.output
      end
    end
    csv << headings

    # Get the data
    collection.each do |obj|
      vals = []
      methods.each do |m|
        chain = m.split('.').reject(&:blank?)
        if chain.size > 1
          val = obj
          chain.each do |c|
            args = c[/\(.*?\)/]
            args = args.nil? ? [] : args[1..-2].split(',').map { |s| eval(s) }
            method = c.split('(').first
            val = args.size > 0 ? val.send(method, *args) : val.send(method)
          end
          vals << val
        else
          vals << obj.send(m)
        end
      end
      csv << vals
    end
  end
end

#view(name) ⇒ Object

Gets all the data from the desired config file, which are further broken up in other methods



43
44
45
# File 'app/presenters/cambium/admin_presenter.rb', line 43

def view(name)
  load_config(name).nil? ? nil : load_config(name).to_ostruct
end