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
157
158
159
160
161
162
# File 'app/controllers/virtual_classes_controller.rb', line 136

def create
  attrs = params[:virtual_class]
  type = attrs.delete(:type)
  if type == 'Role'
    # Remove invalid attributes
    accessible = ::Role.accessible_attributes
    attrs.each do |k,v|
      attrs.delete(k) if v.blank? or !accessible.include?(k)
    end
    @virtual_class = ::Role.new(attrs)
  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



179
180
181
182
183
184
185
186
187
# File 'app/controllers/virtual_classes_controller.rb', line 179

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



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

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



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 52

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)[^>]*)>/, '_ZYAML__\2_')
      #@diff.gsub!(/<\/(ins|del)>/, '_ZYAMLC__\1_')
      #@diff = Zena::CodeSyntax.new(@diff, 'yaml').to_html
      #@diff.gsub!(/_ZYAML__(\w+)_/, '<\1 class="differ">')
      #@diff.gsub!(/_ZYAMLC__(\w+)_/, '</\1>')
    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
46
# 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
  
  list = @virtual_classes.map(&:name)
  if last = @virtual_classes.last
    last_kpath = last.kpath
    Node.native_classes.each do |kpath, klass|
      if kpath < last_kpath && !list.include?(klass.name)
        @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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/virtual_classes_controller.rb', line 164

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