Class: IshManager::MapsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #home, #tinymce

Instance Method Details

#createObject

alphabetized



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/ish_manager/maps_controller.rb', line 9

def create
  @map = ::Gameui::Map.new(map_params)

  if params[:image]
    image = ::Ish::ImageAsset.new :image => params[:image]
    @map.image = image
    image.save
  end

  if map_params[:parent_slug].present?
    @map.parent = ::Gameui::Map.find_by({ slug: map_params[:parent_slug] })
  end
  authorize! :create, @map

  if @map.save
    redirect_to map_path(@map), notice: 'Map was successfully created.'
  else
    render :new
  end
end

#destroyObject



30
31
32
33
34
35
36
# File 'app/controllers/ish_manager/maps_controller.rb', line 30

def destroy
  authorize! :destroy, @map
  @map.destroy
  respond_to do |format|
    format.html { redirect_to maps_path, notice: 'Map was successfully destroyed.' }
  end
end

#editObject

@TODO: @obsolete, remove



39
40
41
42
# File 'app/controllers/ish_manager/maps_controller.rb', line 39

def edit
  authorize! :edit, @map
  redirect_to action: 'show'
end

#exportObject



44
45
46
47
48
# File 'app/controllers/ish_manager/maps_controller.rb', line 44

def export
  authorize! :edit, @map
  result = @map.export_subtree
  send_data result
end

#importObject

@TODO: move to models, yes?



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
81
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/ish_manager/maps_controller.rb', line 53

def import
  authorize! :create, Map

  file_data = params[:input]
  if file_data.respond_to?(:read)
    contents = file_data.read
  elsif file_data.respond_to?(:path)
    contents = File.read(file_data.path)
  else
    logger.error "Bad file_data: #{file_data.class.name}: #{file_data.inspect}"
  end
  contents = JSON.parse contents
  contents.deep_symbolize_keys!

  ## Delete existing
  if params[:delete_existing]
    map_ids = contents[:maps].map { |m| m[:_id] }
    marker_ids = contents[:markers].map { |m| m[:_id] }

    maps = Map.where( :_id.in => map_ids )
    maps.each do |m|
      marker_ids += m.markers.map(&:_id)
    end
    maps.destroy_all

    Marker.where( :_id.in => marker_ids ).destroy_all
  end


  ##
  ## process content
  ##
  errors = []

  # profiles
  profiles = contents[:profiles]
  contents.delete :profiles
  profiles.each do |profile|
    if Ish::UserProfile.where( _id: profile[:_id] ).first
      errors.push({ message: "profile #{profile[:email]} already exists." })
    else
      p = Profile.new({ email: profile[:email], _id: profile[:_id] })
      u = User.where( email: profile[:email] ).first
      u ||= User.new( email: profile[:email], password: rand.to_s )
      flag = u.save && p.save
      if flag
        errors.push({ message: "Profile created for #{profile[:email]}." })
      else
        errors.push({ message: u.errors.full_messages.join(", ") })
        errors.push({ message: p.errors.full_messages.join(", ") })
      end
    end
  end

  # everything else
  contents.each do |k, v|
    # puts! [k, v], "Importing"
    item = Map.export_key_to_class[k].constantize
    v.map do |inn|
      n = item.new inn
      begin
        flag = n.save
      rescue Mongo::Error::OperationFailure => e
        errors.push({ class: k, id: inn[:_id], messages: "Mongo::Error::OperationFailure :: |#{e.to_s}|" })
      end
      if flag
        errors.push({ class: k, id: inn[:_id], messages: 'ok' })
      else
        errors.push({ class: k, id: inn[:_id], messages: "Could not save: |#{n.errors.full_messages.join(", ")}|." })
      end
    end
  end

  flash[:notice] = errors
  redirect_to action: :index
end

#indexObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/ish_manager/maps_controller.rb', line 130

def index
  authorize! :index, ::Gameui::Map

  if params[:q]
    @maps = ::Gameui::Map.or({ slug: /#{params[:q]}/i }, { name: /#{params[:q]}/i })
    if @maps.length == 1
      redirect_to map_path(@maps[0])
      return
    end
  end

  @maps ||= ::Gameui::Map.unscoped.where( parent_slug: "" ).order( slug: :asc )
  @all_maps = Gameui::Map.all.order( slug: :asc )
end

#map_editorObject



145
146
147
# File 'app/controllers/ish_manager/maps_controller.rb', line 145

def map_editor
  authorize! :update, @map
end

#newObject



149
150
151
152
# File 'app/controllers/ish_manager/maps_controller.rb', line 149

def new
  authorize! :new, ::Gameui::Map
  @map = ::Gameui::Map.new
end

#showObject



154
155
156
157
# File 'app/controllers/ish_manager/maps_controller.rb', line 154

def show
  authorize! :show, @map
  @maps = Gameui::Map.where( parent_slug: @map.slug )
end

#updateObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/ish_manager/maps_controller.rb', line 159

def update
  authorize! :update, @map

  if params[:image]
    image = ::Ish::ImageAsset.new :image => params[:image]
    @map.image = image
    image.save
  end

  @map.config = JSON.parse map_params[:config]

  # And update markers
  # @TODO: rewrite and make this one query!
  if map_marker_params.present?
    markers = Marker.where( destination: @map )
    markers.each do |m|
      map_marker_params.each do |k, v|
        m.update_attribute(k, v)
      end
    end
  end

  if map_params[:parent_slug].present?
    @map.parent = ::Gameui::Map.find_by({ slug: map_params[:parent_slug] })
  else
    @map.parent = nil
  end

  respond_to do |format|
    if @map.update(map_params)
      format.html do # format is required
        redirect_to edit_map_path(@map), notice: 'Map was successfully updated.'
      end
    else
      format.html do
        render :edit
      end
    end
  end
end