Class: HelpAnywhere::ResourcesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/help_anywhere/resources_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/help_anywhere/resources_controller.rb', line 62

def create
  resource = HelpAnywhere::Resource.new
  resource.name = params[:name]

  if resource.save
    render :json => {
      :pages => resource.pages.map{ |x|
        {
          :order => x.number,
          :items => x.items
        }
      }
    }
  else
    render :json => { :status => "BAD", :errors => resource.errors }
  end

end

#deleteObject



81
82
83
84
# File 'app/controllers/help_anywhere/resources_controller.rb', line 81

def delete
  resource = HelpAnywhere::Resource.find params[:name]
  resource.destroy!
end

#saveObject



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
# File 'app/controllers/help_anywhere/resources_controller.rb', line 23

def save
  resource = HelpAnywhere::Resource.find_by_name params[:name]

  if resource.nil?
    resource = HelpAnywhere::Resource.new
    resource.name = params[:name]
    resource.save!
  end

  page_list = JSON.parse(params[:content])

  resource.transaction do
    resource.pages.each do |page_to_remove|
      page_to_remove.destroy
    end

    page_list.each do |page|
      page = page.with_indifferent_access
      page_model = HelpAnywhere::Page.new
      page_model.resource = resource
      page_model.save!

      page[:items].each do |item|
        it = HelpAnywhere::Item.new

        it.class_id = item[:class_id]
        it.content = item[:content].to_json
        it.page = page_model

        it.save!
      end

      page_model.save!
    end
  end

  render :json => {status: 'OK'}
end

#showObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/help_anywhere/resources_controller.rb', line 2

def show
  resource = HelpAnywhere::Resource.find_by_name params[:name]

  if resource.nil?
    render :json => {
      :pages => []
    }
  else
    render :json => {
      :pages => resource.pages.order('number ASC').map{ |x|
        {
          :order => x.number,
          :items => x.items.map{ |x|
            { :id => x.id, :class_id => x.class_id, :content => JSON.parse(x.content || '{}') }
          }
        }
      }
    }
  end
end