Class: VersionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/versions_controller.rb

Instance Method Summary collapse

Methods included from Zena::App

included

Instance Method Details

#css_previewObject

This is a helpers used when creating the css for the site. They have no link with the database



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/versions_controller.rb', line 174

def css_preview
  file = params[:css].gsub('..','')
  path = File.join(Zena::ROOT, 'public', 'stylesheets', file)
  if File.exists?(path)
    if session[:css] && session[:css] == File.stat(path).mtime
      render :nothing=>true
    else
      session[:css] = File.stat(path).mtime
      @css = File.read(path)
    end
  else
    render :nothing=>true
  end
end

#custom_tabObject



85
86
87
88
89
# File 'app/controllers/versions_controller.rb', line 85

def custom_tab
  render :file => template_url(:mode=>'+edit', :format=>'html'), :layout=>false
rescue ActiveRecord::RecordNotFound
  render :action => 'custom_tab'
end

#destroyObject

TODO: test



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/controllers/versions_controller.rb', line 248

def destroy
  if @node.destroy_version
    if @node.versions.empty?
      flash.now[:notice] = _("Node destroyed.")
      respond_to do |format|
        format.html { redirect_to zen_path(@node.parent) }
        format.js
      end
    else
      flash.now[:notice] = _("Version destroyed.")
      do_rendering
    end
  else
    flash.now[:error] = _("Could not destroy version.")
    do_rendering
  end
end

#diffObject

TODO: test/improve or remove (experiments) github.com/samg/diffy



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/versions_controller.rb', line 93

def diff
  if false
    # FULL PAGE DIFF
    
    # Render with source
    source_html = render_and_cache :as_string => true

    # target
    if params[:to].to_i > 0
      version = secure!(Version) { Version.find(:first, :conditions => ['node_id = ? AND number = ?', @node.id, params[:to]])}
      # Reload prop
      @node.prop = version.prop
    else
      # default
      @node.instance_variable_set(:@version, nil)
    end

    # Render with target
    target_html = render_and_cache :as_string => true
    
    result = Differ.diff_by_word(target_html, source_html).format_as(:html)
    
    render :inline => result # Broken because Differ does not understand html...
  else
    # PROP DIFF

    # source
    source = @node.version.prop

    # target
    if params[:to].to_i > 0
      version = secure!(Version) { Version.find(:first, :conditions => ['node_id = ? AND number = ?', @node.id, params[:to]])}
      @node.version = version
    else
      # default
      @node.instance_variable_set(:@version, nil)
    end
    target = @node.prop

    keys = (target.keys + source.keys).uniq

    keys.each do |k|
      target[k] = Differ.diff_by_word(
        target[k] || '',
        source[k] || '').format_as(:html).gsub(/(\s+)<\/del>/, '</del>\1')
    end

    show
  end
end

#editObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/versions_controller.rb', line 57

def edit
  if params[:drive]
    if @node.redit
      flash.now[:notice] = _("Version changed back to redaction.")
    else
      flash.now[:error] = _("Could not change version back to redaction.")
    end
    render :action=>'update'
  else
    @title_for_layout = title_for_layout
    if @node.kind_of?(TextDocument)
      if params['parse_assets']
        @node.text = @node.parse_assets(@node.text, self, 'text')
      elsif @node.kind_of?(TextDocument) && params['unparse_assets']
        @node.text = @node.unparse_assets(@node.text, self, 'text')
      end
    end
    @edit = true
  end
  if params[:close] == 'true'
    js_data << "Zena.reload_and_close();"
  else
    js_data << <<-END_TXT
    Zena.editor_setup('#{preview_node_version_path(:node_id=>@node[:zip], :id=>(@node.version.number || 0), :escape => false)}');
    END_TXT
  end
end

#previewObject

preview when editing node



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/versions_controller.rb', line 145

def preview
  if @key = (params['key'] || params['amp;key'])
    if @node.can_write?
      @value = params[:content]
      if @node.kind_of?(TextDocument) && @key == 'text'
        l = @node.content_lang
        @value = "<code#{l ? " lang='#{l}'" : ''} class=\'full\'>#{@value}</code>"
      end
    else
      @value = "<span style='background:#f66;padding:10px; border:1px solid red; color:black;font-size:10pt;font-weight:normal;'>#{_('you do not have write access here')}#{visitor.is_anon? ? " (<a style='color:#00a;text-decoration:underline;' href='/login'>#{_('please login')}</a>)" : ""}</span>"
    end
  else
    # elsif @node.kind_of?(Image)
    #   # view image version
    #   # TODO: how to show the image data of a version ? 'nodes/3/versions/4.jpg' ?
    #   @node.text = "<img src='#{url_for(:controller=>'versions', :node_id=>@node[:zip], :id=>@node.v_number, :format=>@node.ext)}'/>"
    # elsif @node.kind_of?(TextDocument)
    #   lang = @node.content_lang
    #   lang = lang ? " lang='#{lang}'" : ""
    #   @node.text = "<code#{lang} class='full'>#{@text}</code>"
  end


  respond_to do |format|
    format.js
  end
end

#proposeObject



189
190
191
192
193
194
195
196
# File 'app/controllers/versions_controller.rb', line 189

def propose
  if @node.propose
    flash.now[:notice] = _("Redaction proposed for publication.")
  else
    flash.now[:error] = _("Could not propose redaction.")
  end
  do_rendering
end

#publishObject



208
209
210
211
212
213
214
215
216
# File 'app/controllers/versions_controller.rb', line 208

def publish
  if @node.publish
    flash.now[:notice] = _("Redaction published.")
  else
    flash.now[:error] = _("Could not publish:") +
      " #{error_messages_for(:node, :object => @node)}"
  end
  do_rendering
end

#reditObject



227
228
229
230
231
232
233
234
235
# File 'app/controllers/versions_controller.rb', line 227

def redit
  if @node.redit
    flash.now[:notice] = _("Rolled back to redaction.")
  else
    flash.now[:error] = _("Could not rollback:") +
      " #{error_messages_for(:node, :object => @node)}"
  end
  do_rendering
end

#refuseObject



198
199
200
201
202
203
204
205
206
# File 'app/controllers/versions_controller.rb', line 198

def refuse
  if @node.refuse
    flash.now[:notice] = _("Proposition refused.")
    @redirect_url = @node.can_read? ? request.env['HTTP_REFERER'] : user_path(visitor)
  else
    flash.now[:notice] = _("Could not refuse proposition.")
  end
  do_rendering
end

#removeObject



218
219
220
221
222
223
224
225
# File 'app/controllers/versions_controller.rb', line 218

def remove
  if @node.remove
    flash.now[:notice] = _("Publication removed.")
  else
    flash.now[:error] = _("Could not remove publication.")
  end
  do_rendering
end

#showObject

Display a specific version of a node



7
8
9
10
11
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
# File 'app/controllers/versions_controller.rb', line 7

def show
  respond_to do |format|

    format.html {
      if @node.id == current_site.home_id
        render_and_cache :cache => false, :mode => '+index'
        insert_warning
      else
        render_and_cache :cache => false
        insert_warning
      end
    }

    format.xml  { render :xml => @node.to_xml }

    format.js # show.rjs

    format.all  do
      # Get document data (inline if possible)
      if params[:format] != @node.safe_content_read('ext')
        return redirect_to(params.merge(:format => (@node.safe_content_read('ext') || 'html')))
      end

      if @node.kind_of?(Image) && !Zena::Use::ImageBuilder.dummy?
        img_format = Iformat[params[:mode]]
        data = @node.file(img_format)
        content_path = @node.filepath(img_format)
        disposition  = 'inline'

      elsif @node.kind_of?(TextDocument)
        data = StringIO.new(@node.text)
        content_path = nil
        disposition  = 'attachment'

      else
        data         = @node.file
        content_path = @node.filepath
        disposition  = 'inline'
      end
      raise ActiveRecord::RecordNotFound unless data

      send_data( data.read , :filename => @node.filename, :type => @node.content_type, :disposition => disposition)
      data.close

      # should we cache the page ?
      # cache_page(:content_path => content_path) # content_path is used to cache by creating a symlink
    end
  end
end

#unpublishObject

TODO: test



238
239
240
241
242
243
244
245
# File 'app/controllers/versions_controller.rb', line 238

def unpublish
  if @node.unpublish
    flash.now[:notice] = _("Publication removed.")
  else
    flash.now[:error] = _("Could not remove publication.")
  end
  do_rendering
end