Class: VirtualClassesController

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

Instance Method Summary collapse

Methods included from Zena::App

included

Instance Method Details

#createObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/virtual_classes_controller.rb', line 136

def create
  type = params[:virtual_class].delete(:type)
  if type == 'Role'
    @virtual_class = ::Role.new(params[:virtual_class])
  else
    @virtual_class = VirtualClass.new(params[:virtual_class])
  end

  respond_to do |format|
    if @virtual_class.save
      flash.now[:notice] = _('VirtualClass was successfully created.')
      format.html { redirect_to virtual_class_url(@virtual_class) }
      format.js
      format.xml  { render :xml => @virtual_class, :status => :created, :location => virtual_class_url(@virtual_class) }
    else
      format.html { render :action => "new" }
      format.js
      format.xml  { render :xml => @virtual_class.errors }
    end
  end
end

#destroyObject



173
174
175
176
177
178
179
180
181
# File 'app/controllers/virtual_classes_controller.rb', line 173

def destroy
  @virtual_class.destroy

  respond_to do |format|
    format.html { redirect_to virtual_classes_url }
    format.xml  { head :ok }
    format.js
  end
end

#editObject

TODO: test



129
130
131
132
133
134
# File 'app/controllers/virtual_classes_controller.rb', line 129

def edit
  respond_to do |format|
    format.html { render :partial => 'virtual_classes/form' }
    format.js   { render :partial => 'virtual_classes/form', :layout => false }
  end
end

#exportObject



47
48
49
# File 'app/controllers/virtual_classes_controller.rb', line 47

def export
  send_data(clean_yaml(::Role.export), :filename=>"roles.yml", :type => 'text/yaml')
end

#importObject



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
# File 'app/controllers/virtual_classes_controller.rb', line 82

def import
  data = YAML.load(params[:roles]) rescue nil
  if data.nil?
    flash.now[:error] = _("Could not parse yaml document")
    redirect_to :action => :index
  else
    @roles_backup    = clean_yaml(::Role.export)
    @virtual_classes = ::Role.import(data)
    class << @virtual_classes
      def total_pages; 1 end
    end
    @virtual_class   = VirtualClass.new('')
    respond_to do |format|
      format.html { render :action => 'index' }
    end
  end
rescue ActiveRecord::RecordInvalid => e
  r = e.record
  if r.respond_to?(:name)
    flash[:error] = "#{r.class} '#{r.name}' #{e.message}"
  else
    flash[:error] = "#{r.class} '#{r.inspect}' #{e.message}"
  end
  redirect_to :action => :index
rescue Exception => e
  flash.now[:error] = e.message
  redirect_to :action => :index
end

#import_prepareObject



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
80
# File 'app/controllers/virtual_classes_controller.rb', line 51

def import_prepare
  attachment = params[:attachment]
  if attachment.nil?
    flass[:error] = _("Upload failure: no definitions.")
    redirect_to :action => :index
  else
    @yaml = attachment.read rescue nil
    data = YAML.load(@yaml) rescue nil
    if data.nil?
      flash.now[:error] = "Could not parse yaml document"
      redirect_to :action => :index
    else
      @yaml.gsub!(/\A---\s*\n/, "--- \n")
      current = current_compared_to(data)
      a = clean_yaml(current).gsub('{}', '')
      #a = Zena::CodeSyntax.new(a, 'yaml').to_html
      b = @yaml
      #b = Zena::CodeSyntax.new(b, 'yaml').to_html
      @diff = Differ.(b, a).format_as(:html)

      # UGLY HACK to not escape <ins> and <del>
      @diff.gsub!(/<((ins|del)[^>]*)>/, '[yaml_diff[\1]]')
      @diff.gsub!(/<\/(ins|del)>/, '[yaml_diff[/\1]]')
      @diff = Zena::CodeSyntax.new(@diff, 'yaml').to_html
      @diff.gsub!(/\[yaml_diff\[([^\]]+)\]\]/) do
        "<#{$1.gsub('&quot;', '"')}>"
      end
    end
  end
end

#indexObject



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
# File 'app/controllers/virtual_classes_controller.rb', line 9

def index
  secure(::Role) do
    @virtual_classes = ::Role.paginate(:all, :order => 'kpath', :per_page => 200, :page => params[:page])
  end

  if last = @virtual_classes.last
    last_kpath = last.kpath
    Node.native_classes.each do |kpath, klass|
      if kpath < last_kpath
        @virtual_classes << klass
      end
    end
  else
    Node.native_classes.each do |kpath, klass|
      @virtual_classes << klass
    end
  end

  @virtual_classes.sort! do |a, b|
    if a.kpath == b.kpath
      # Order VirtualClass first
      b_type = b.kind_of?(::Role) ? b.class.to_s : 'V' # sort real classes like VirtualClass
      a_type = a.kind_of?(::Role) ? a.class.to_s : 'V'

      b_type <=> a_type
    else
      a.kpath <=> b.kpath
    end
  end

  @virtual_class  = VirtualClass.new('')

  respond_to do |format|
    format.html # index.erb
    format.xml  { render :xml => @virtual_classes }
  end
end

#newObject



119
120
121
122
123
124
125
126
# File 'app/controllers/virtual_classes_controller.rb', line 119

def new
  @virtual_class = VirtualClass.new('')

  respond_to do |format|
    format.html # new.erb
    format.xml  { render :xml => @virtual_class }
  end
end

#showObject



111
112
113
114
115
116
117
# File 'app/controllers/virtual_classes_controller.rb', line 111

def show
  respond_to do |format|
    format.html # show.erb
    format.js
    format.xml  { render :xml => @virtual_class }
  end
end

#updateObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/virtual_classes_controller.rb', line 158

def update
  respond_to do |format|
    if @virtual_class.update_attributes(params[:virtual_class])
      flash.now[:notice] = _('VirtualClass was successfully updated.')
      format.html { redirect_to virtual_class_url(@virtual_class) }
      format.js
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.js
      format.xml  { render :xml => @virtual_class.errors }
    end
  end
end