Class: GraphStarter::AssetsController

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

Instance Method Summary collapse

Instance Method Details

#assetObject



60
61
62
# File 'app/controllers/graph_starter/assets_controller.rb', line 60

def asset
  model_class_scope.find(params[:id])
end

#asset_setObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/graph_starter/assets_controller.rb', line 24

def asset_set
  associations = []
  associations << :images if images?
  associations << model_class.category_association if model_class.category_association

  scope = model_class_scope
  scope = yield scope if block_given?

  scope = scope.limit(50)

  if associations.present?
    scope.query_as(:s).with(:s).proxy_as(model_class_scope.model, :s).with_associations(*associations)
  else
    scope
  end
end

#editObject



47
48
49
50
51
# File 'app/controllers/graph_starter/assets_controller.rb', line 47

def edit
  @asset = asset

  render file: 'public/404.html', status: :not_found, layout: false if !@asset
end

#homeObject



3
4
# File 'app/controllers/graph_starter/assets_controller.rb', line 3

def home
end

#images?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/controllers/graph_starter/assets_controller.rb', line 64

def images?
  model_class_scope.has_images?
end

#indexObject



6
7
8
# File 'app/controllers/graph_starter/assets_controller.rb', line 6

def index
  @assets = asset_set
end

#model_class_scopeObject



68
69
70
71
72
73
74
# File 'app/controllers/graph_starter/assets_controller.rb', line 68

def model_class_scope
  @model_class_scope = if defined?(current_user)
    model_class.authorized_for(current_user)
  else
    model_class.all
  end
end

#searchObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/graph_starter/assets_controller.rb', line 10

def search
  regexp = Regexp.new('.*' + params[:query].gsub(/\s+/, '.*') + '.*', 'i')
  assets = asset_set { |scope| scope.where(title: regexp) }
  results_data = assets.map do |asset|
    {
      title: asset.title,
      url: asset_path(id: asset, model_slug: asset.class.model_slug),
      image: images? && asset.first_image_source && asset.first_image_source.url || nil
    }.reject {|_, v| v.nil? }
  end

  render json: {results: results_data}.to_json
end

#showObject



41
42
43
44
45
# File 'app/controllers/graph_starter/assets_controller.rb', line 41

def show
  @asset = asset

  render file: 'public/404.html', status: :not_found, layout: false if !@asset
end

#updateObject



53
54
55
56
57
58
# File 'app/controllers/graph_starter/assets_controller.rb', line 53

def update
  @asset = asset
  @asset.update(params[:book])

  redirect_to action: :edit
end