Class: SitemapsController

Inherits:
InheritedResources::Base
  • Object
show all
Defined in:
app/controllers/sitemaps_controller.rb

Instance Method Summary collapse

Instance Method Details

#authorize_action!Object

helper methods



94
95
96
# File 'app/controllers/sitemaps_controller.rb', line 94

def authorize_action!
  authorize! :manage, Sitemap
end

#collectionObject



102
103
104
# File 'app/controllers/sitemaps_controller.rb', line 102

def collection
  @sitemaps ||= SitemapArray.new(parent.children)
end

#createObject



23
24
25
26
27
28
29
30
# File 'app/controllers/sitemaps_controller.rb', line 23

def create
  @sitemap = new_resource
  @sitemap.resource = @sitemap.resource_type.constantize.new
  @sitemap.save
  respond_with @sitemap do |format|
    format.json {render :json=> json_obj.as_json}
  end
end

#destroyObject



57
58
59
60
61
62
63
64
65
# File 'app/controllers/sitemaps_controller.rb', line 57

def destroy
  if (resource.parent == Sitemap.lost_and_found) || (resource.resource.is_a? AppConfig)
    resource.destroy
  else
    resource.parent = Sitemap.lost_and_found
    resource.save
  end
  respond_with resource
end

#indexObject



10
11
12
13
14
15
# File 'app/controllers/sitemaps_controller.rb', line 10

def index
  collection.reject!{|t| t==Sitemap.lost_and_found} if Sitemap.lost_and_found.children.empty?
  index! do |format|
    format.json{render :json=>collection.as_json}
  end
end

#json_objObject



32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/sitemaps_controller.rb', line 32

def json_obj
  {
    :status => @sitemap.errors.empty? ? 1 : 0,
    :errors => @sitemap.errors,
    :id     => @sitemap.id,
    :resource_type => @sitemap.resource_type,
    :resource_id   => @sitemap.resource_id,
    :menu_text     => @sitemap.menu_text,
  }
end

#new_resourceObject



106
107
108
# File 'app/controllers/sitemaps_controller.rb', line 106

def new_resource
  @sitemap ||= Sitemap.new(params[:sitemap])
end

#parentObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/sitemaps_controller.rb', line 110

def parent
  unless @parent
    if params[:parent_id]
      @parent = Sitemap.find(params[:parent_id])
    else
      if params[:action] == "index"
        @parent = Sitemap.root
      else
        @parent = resource.try(:parent)
      end
    end
  end
  @parent
end

#parse_request_urlObject



17
18
19
20
21
# File 'app/controllers/sitemaps_controller.rb', line 17

def parse_request_url
  if params[:level1]
    @sitemap = Sitemap.find_by_path(*[1,2,3,4].map{|t|params["level#{i}".to_sym]})
  end
end

#resourceObject



98
99
100
# File 'app/controllers/sitemaps_controller.rb', line 98

def resource
  @sitemap ||= Sitemap.find(params[:id]) if params[:id]
end

#showObject



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

def show
  show! do |format|
    format.html{
      if resource.resource.is_a? ExternalLink
        redirect_to resource.resource.url
      elsif resource.resource.is_a? Page

      end
    }
    format.json{render :json=>resource.as_json}
    format.js{redirect_to(url_for(resource.resource)+'.js')}
  end
end

#sortObject



83
84
85
86
87
88
# File 'app/controllers/sitemaps_controller.rb', line 83

def sort
  params[:formula_herb_children].each_with_index do |id, index|
    FormulaHerb.update_all(['position=?', index+1], ['id=?', id])
  end
  render :nothing => true
end

#updateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/sitemaps_controller.rb', line 67

def update
  resource.parent = parent
  update! do |format|
    if params.stringify_keys["sitemap"]["position"]
      childrunz = Array.new(parent.children.sorted)
      childrunz.delete(resource)
      childrunz.insert(params.stringify_keys["sitemap"]["position"].to_i, resource)
      childrunz.each_with_index do |child, idx|
        child.position = idx
        child.save
      end
    end
    format.json {render :json=> json_obj.as_json}
  end
end