Class: CortexReaver::AdminController

Inherits:
Controller
  • Object
show all
Defined in:
lib/cortex_reaver/controller/admin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.jobsObject



15
16
17
# File 'lib/cortex_reaver/controller/admin.rb', line 15

def self.jobs
  @jobs ||= {}
end

Instance Method Details

#configurationObject



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cortex_reaver/controller/admin.rb', line 22

def configuration
  if request.post?
    begin
      # Update config
      c = CortexReaver.config
      errors = {}
      
      # Site
      c.site.url = request['site.url']
      c.site.name = request['site.name']
      c.site.description = request['site.description']
      c.site.keywords = request['site.keywords']
      c.site.author = request['site.author']
      
      errors['site.url'] = "isn't a URL" unless c.site.url =~ /^http:\/\/.+/
      errors['site.name'] = "is blank" if c.site.name.blank?
      errors['site.author'] = "is blank" if c.site.author.blank?

      # View sections
      c.view.sections = []
      request['view.sections'].split("\n").each do |line|
        parts = line.strip.split(' ')
        if parts.size > 1
          c.view.sections << [parts[0..-2].join(' '), parts[-1]]
        end
      end

      # Photographs
      c.photographs.sizes = Construct.new
      request['photographs.sizes'].split("\n").each do |line|
        parts = line.strip.split(' ', 2)
        if parts.size > 1
          c.photographs.sizes[parts.first] = parts.last
        end
      end

      if errors.empty?
        # Save
        CortexReaver.instance_variable_set '@config', c
        CortexReaver.config.save
        flash[:notice] = "Configuration saved."
        redirect rs
      else
        flash[:error] = "Configuration errors."
        @config = c
        @errors = errors
      end
    rescue => e
      Ramaze::Log.error e.inspect + e.backtrace.join("\n")
      flash[:error] = "Unable to update configuration: #{h e}"
      @config = c
      @errors = {}
    end
  else
    @config = CortexReaver.config
    @errors = {}
  end
end

#indexObject



19
20
# File 'lib/cortex_reaver/controller/admin.rb', line 19

def index
end

#regenerate_cachesObject



100
101
102
103
104
105
# File 'lib/cortex_reaver/controller/admin.rb', line 100

def regenerate_caches
  [Journal, Page, Comment].each do |klass|
    klass.refresh_render_caches
  end
  redirect rs()
end

#regenerate_photo_sizesObject

Regenerates thumbnails on photographs



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cortex_reaver/controller/admin.rb', line 116

def regenerate_photo_sizes
  unless self.class.jobs[:regenerate_photo_sizes]
    self.class.jobs[:regenerate_photo_sizes] = Thread.new do
      Thread.current.priority = -2
      Thread.current[:total] = Photograph.count
      while photo = (photo ? photo.next : Photograph.first)
        Thread.current[:photo] = photo
        Thread.current[:i] = photo.position
        photo.regenerate_sizes
      end

      # Done
      self.class.jobs[:regenerate_photo_sizes] = nil
      Thread.exit
    end
  end
end

#regenerate_photo_sizes_statusObject



107
108
109
110
111
112
113
# File 'lib/cortex_reaver/controller/admin.rb', line 107

def regenerate_photo_sizes_status
  if job = self.class.jobs[:regenerate_photo_sizes]
    respond "{'i':#{job[:i]},'total':#{job[:total]}}"
  else
    respond '{}'
  end
end

#update_commentsObject

Recalculate comment counts



82
83
84
85
86
87
88
89
# File 'lib/cortex_reaver/controller/admin.rb', line 82

def update_comments
  [Journal, Page, Photograph].each do |klass|
    klass.refresh_comment_counts
  end

  flash[:notice] = "Comment counts updated."
  redirect rs()
end

#update_tagsObject

Recalculate tag counts and vacuum unused tags



92
93
94
95
96
97
98
# File 'lib/cortex_reaver/controller/admin.rb', line 92

def update_tags
  @updated = Tag.refresh_counts
  @deleted = []
  Tag.unused.order(:title).all.each do |tag|
    @deleted << tag.destroy
  end
end