Class: GraphStarter::AssetsController

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

Instance Method Summary collapse

Instance Method Details

#assetObject



83
84
85
# File 'app/controllers/graph_starter/assets_controller.rb', line 83

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

#asset_set(var = :asset) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/graph_starter/assets_controller.rb', line 34

def asset_set(var = :asset)
  associations = []
  associations << :images if images?
  associations << model_class.category_association if model_class.category_association

  scope = model_class_scope(var)
  scope = yield scope if block_given?

  scope = scope.limit(50)

  if associations.present?
    scope.query_as(var).with(var).proxy_as(model_class, var).with_associations(*associations)
  else
    scope
  end
end

#editObject



57
58
59
60
61
# File 'app/controllers/graph_starter/assets_controller.rb', line 57

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)


87
88
89
# File 'app/controllers/graph_starter/assets_controller.rb', line 87

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_scope(var = :asset) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/controllers/graph_starter/assets_controller.rb', line 91

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

  @model_class_scope ||= model_class.all(var)
end

#rateObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/graph_starter/assets_controller.rb', line 70

def rate
  if current_user
    rating = asset.rating_for(current_user)
    rating ||= Rating.find(from_node: current_user, to_node: asset)

    rating.update_attribute(:level, params[:new_rating])

    render json: rating
  else
    render json: {}
  end
end

#searchObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/graph_starter/assets_controller.rb', line 10

def search
  assets = asset_set { |scope| scope.for_query(params[:query]) }
  results_data = assets.map do |asset|
    description = model_class.search_properties.map do |property|
      "<b>#{property.to_s.humanize}:</b> #{asset.read_attribute(property)}"
    end.join("<br>")

    {
      title: asset.title,
      url: asset_path(id: asset, model_slug: asset.class.model_slug),
      description: description,
      image: images? && asset.first_image_source_url || nil
    }.reject {|_, v| v.nil? }.tap do |result|
      model_class.search_properties.each do |property|
        result[property] = asset.read_attribute(property)
      end
    end
  end

  # INCLUDE SEARCH QUERY PROPERTIES IN RESULT!!!

  render json: {results: results_data}.to_json
end

#showObject



51
52
53
54
55
# File 'app/controllers/graph_starter/assets_controller.rb', line 51

def show
  @asset = asset

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

#updateObject



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

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

  redirect_to action: :edit
end