diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/related_assets_controller.rb | 50 | ||||
| -rw-r--r-- | app/models/node.rb | 4 |
2 files changed, 54 insertions, 0 deletions
diff --git a/app/controllers/related_assets_controller.rb b/app/controllers/related_assets_controller.rb new file mode 100644 index 0000000..906c02c --- /dev/null +++ b/app/controllers/related_assets_controller.rb | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | class RelatedAssetsController < ApplicationController | ||
| 2 | before_action :login_required | ||
| 3 | before_action :find_node | ||
| 4 | |||
| 5 | def search | ||
| 6 | term = params[:q].to_s.strip | ||
| 7 | if term.blank? | ||
| 8 | render json: [] | ||
| 9 | return | ||
| 10 | end | ||
| 11 | |||
| 12 | attached_ids = @node.editable_page.related_assets.pluck(:asset_id) | ||
| 13 | results = Asset.images | ||
| 14 | .where("name ILIKE ?", "%#{term}%") | ||
| 15 | .where.not(id: attached_ids) | ||
| 16 | .limit(10) | ||
| 17 | |||
| 18 | render json: results.map { |a| | ||
| 19 | { id: a.id, name: a.name, thumb_url: a.upload.url(:thumb) } | ||
| 20 | } | ||
| 21 | end | ||
| 22 | |||
| 23 | def create | ||
| 24 | asset = Asset.find(params[:asset_id]) | ||
| 25 | related = @node.editable_page.related_assets.find_or_create_by!(asset: asset) | ||
| 26 | |||
| 27 | render json: { | ||
| 28 | id: related.id, | ||
| 29 | asset_id: asset.id, | ||
| 30 | name: asset.name, | ||
| 31 | thumb_url: asset.upload.url(:thumb) | ||
| 32 | } | ||
| 33 | end | ||
| 34 | |||
| 35 | def destroy | ||
| 36 | @node.editable_page.related_assets.find(params[:id]).destroy | ||
| 37 | head :ok | ||
| 38 | end | ||
| 39 | |||
| 40 | def update | ||
| 41 | @node.editable_page.related_assets.find(params[:id]).insert_at(params[:position].to_i) | ||
| 42 | head :ok | ||
| 43 | end | ||
| 44 | |||
| 45 | private | ||
| 46 | |||
| 47 | def find_node | ||
| 48 | @node = Node.find(params[:node_id]) | ||
| 49 | end | ||
| 50 | end | ||
diff --git a/app/models/node.rb b/app/models/node.rb index 1d0a089..0361c1e 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -328,6 +328,10 @@ class Node < ApplicationRecord | |||
| 328 | unique_path.length == 3 && unique_path[0] == "updates" | 328 | unique_path.length == 3 && unique_path[0] == "updates" |
| 329 | end | 329 | end |
| 330 | 330 | ||
| 331 | def editable_page | ||
| 332 | autosave || draft || head | ||
| 333 | end | ||
| 334 | |||
| 331 | # Returns immutable node id for all new nodes so that the atom feed entry ids | 335 | # Returns immutable node id for all new nodes so that the atom feed entry ids |
| 332 | # stay the same eventhough the slug or positions changes. | 336 | # stay the same eventhough the slug or positions changes. |
| 333 | # Can be removed after a year or so ;) | 337 | # Can be removed after a year or so ;) |
