Class: TagsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/tags_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#is_data_controller, #is_task_controller, #meta_data, #meta_description, #meta_keywords, #meta_title, #page_title, #site_name

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#clear_project_and_user_variables, #digest_cookie, #digested_cookie_exists?, #disable_turbolinks, #intercept_api, #invalid_object, #is_data_controller?, #is_task_controller?, #log_user_recent_route, #notice_user, #project_matches, #record_not_found, #set_project_and_user_variables, #set_project_from_params, #token_authenticate

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) autocomplete



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/tags_controller.rb', line 82

def autocomplete
  @tags = Queries::TagAutocompleteQuery.new(params.require(:term), project_id: sessions_current_project_id).all

  data = @tags.collect do |t|
    {id: t.id,
     label: ApplicationController.helpers.tag_tag(t),
     response_values: {
         params[:method] => t.id
     },
     label_html: ApplicationController.helpers.tag_tag(t)
    }
  end

  render :json => data
end

- (Object) create

POST /tags POST /tags.json



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/tags_controller.rb', line 29

def create
  @tag = Tag.new(tag_params)
  # redirect_url = (request.env['HTTP_REFERER'].include?(new_tag_path) ? @tag : :back)
  respond_to do |format|
    if @tag.save
      format.html { redirect_to @tag.tag_object.metamorphosize, notice: 'Tag was successfully created.' }
      format.json { render json: @tag, status: :created, location: @tag }
    else
      format.html {
        redirect_to :back, notice: 'Tag was NOT successfully created.'
      }
      format.json { render json: @tag.errors, status: :unprocessable_entity }
    end
  end
end

- (Object) destroy

DELETE /tags/1 DELETE /tags/1.json



61
62
63
64
65
66
67
# File 'app/controllers/tags_controller.rb', line 61

def destroy
  @tag.destroy
  respond_to do |format|
    format.html { redirect_to :back, notice: 'Tag was successfully destroyed.' }
    format.json { head :no_content }
  end
end

- (Object) download

GET /tags/download



99
100
101
# File 'app/controllers/tags_controller.rb', line 99

def download
  send_data Download.generate_csv(Tag.where(project_id: sessions_current_project_id)), type: 'text', filename: "tags_#{DateTime.now.to_s}.csv"
end

- (Object) index

GET /tags GET /tags.json



17
18
19
20
# File 'app/controllers/tags_controller.rb', line 17

def index
  @recent_objects = Tag.recent_from_project_id(sessions_current_project_id).order(updated_at: :desc).limit(10)
  render '/shared/data/all/index'
end

- (Object) list



69
70
71
# File 'app/controllers/tags_controller.rb', line 69

def list
  @tags = Tag.where(project_id: sessions_current_project_id).order(:id).page(params[:page])
end

- (Object) new



6
7
8
9
10
11
12
13
# File 'app/controllers/tags_controller.rb', line 6

def new
  if !Keyword.with_project_id(sessions_current_project_id).any? # if there are none
    @return_path = "/tags/new?tag[tag_object_attribute]=&tag[tag_object_id]=#{params[:tag][:tag_object_id]}&tag[tag_object_type]=#{params[:tag][:tag_object_type]}"
    redirect_to new_controlled_vocabulary_term_path(return_path: @return_path), notice: 'Create a keyword or two first!' and return
  end

  @taggable_object = taggable_object
end

- (Object) search

GET /tags/search



74
75
76
77
78
79
80
# File 'app/controllers/tags_controller.rb', line 74

def search
  if params[:id].blank?
    redirect_to tags_path, notice: 'You must select an item from the list with a click or tab press before clicking show.'
  else
    redirect_to tag_path(params[:id])
  end
end

- (Object) set_tag (private)



105
106
107
# File 'app/controllers/tags_controller.rb', line 105

def set_tag
  @tag = Tag.with_project_id(sessions_current_project_id).find(params[:id])
end

- (Object) tag_object_update



22
23
24
25
# File 'app/controllers/tags_controller.rb', line 22

def tag_object_update 
  taggable_object.update(taggable_object_params)
  redirect_to :back
end

- (Object) tag_params (private)



109
110
111
# File 'app/controllers/tags_controller.rb', line 109

def tag_params
  params.require(:tag).permit(:keyword_id, :tag_object_id, :tag_object_type, :tag_object_attribute)
end

- (Object) taggable_object (private)



121
122
123
# File 'app/controllers/tags_controller.rb', line 121

def taggable_object
  params.require(:tag_object_type).constantize.find(params.require(:tag_object_id))
end

- (Object) taggable_object_params (private)



113
114
115
116
117
118
119
# File 'app/controllers/tags_controller.rb', line 113

def taggable_object_params
  params.require(:taggable_object).permit(
    tags_attributes: [:_destroy, :id, :keyword_id, :position,
                      keyword_attributes: [:name, :definition, :uri, :html_color]

  ])
end

- (Object) update

PATCH/PUT /tags/1 PATCH/PUT /tags/1.json



47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/tags_controller.rb', line 47

def update
  respond_to do |format|
    if @tag.update(tag_params)
      format.html { redirect_to @tag.tag_object.metamorphosize, notice: 'Tag was successfully created.' }
      format.json { render json: @tag, status: :created, location: @tag }
    else
      format.html { redirect_to :back, notice: 'Tag was NOT successfully updated.' }
      format.json { render json: @tag.errors, status: :unprocessable_entity }
    end
  end
end