Class: GraphStarter::AssetsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- GraphStarter::AssetsController
- Defined in:
- app/controllers/graph_starter/assets_controller.rb
Instance Method Summary collapse
- #asset ⇒ Object
- #asset_set ⇒ Object
- #edit ⇒ Object
- #home ⇒ Object
- #images? ⇒ Boolean
- #index ⇒ Object
- #model_class_scope ⇒ Object
- #search ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#asset ⇒ Object
60 61 62 |
# File 'app/controllers/graph_starter/assets_controller.rb', line 60 def asset model_class_scope.find(params[:id]) end |
#asset_set ⇒ Object
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 |
#edit ⇒ Object
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 |
#home ⇒ Object
3 4 |
# File 'app/controllers/graph_starter/assets_controller.rb', line 3 def home end |
#images? ⇒ Boolean
64 65 66 |
# File 'app/controllers/graph_starter/assets_controller.rb', line 64 def images? model_class_scope.has_images? end |
#index ⇒ Object
6 7 8 |
# File 'app/controllers/graph_starter/assets_controller.rb', line 6 def index @assets = asset_set end |
#model_class_scope ⇒ Object
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.(current_user) else model_class.all end end |
#search ⇒ Object
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 |
#show ⇒ Object
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 |
#update ⇒ Object
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 |