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 included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

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, #whitelist_constantize

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) autocomplete



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/tags_controller.rb', line 98

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) batch_remove

POST /tags/batch_remove?keyword_id=123&klass=456



131
132
133
134
135
136
137
# File 'app/controllers/tags_controller.rb', line 131

def batch_remove
  if Tag.batch_remove(params.require(:keyword_id), params.require(:klass))
    render json: {success: true}
  else
    render json: {success: false}
  end
end

- (Object) create

POST /tags POST /tags.json



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/tags_controller.rb', line 39

def create
  @tag = Tag.new(tag_params)
  respond_to do |format|
    if @tag.save
      format.html { redirect_to url_for(@tag.tag_object.metamorphosize),
                                notice: 'Tag was successfully created.' }
      format.json { render action: 'show', status: :created, location: @tag }
    else
      format.html {
        redirect_back(fallback_location: (request.referer || root_path),
                      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



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

def destroy
  @tag.destroy!
  respond_to do |format|
    # TODO: probably needs to be changed with new annotator
    format.html { redirect_back(fallback_location: (request.referer || root_path),
                                notice: 'Tag was successfully destroyed.') }
    format.json { head :no_content }
  end
end

- (Object) download

GET /tags/download



125
126
127
128
# File 'app/controllers/tags_controller.rb', line 125

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

- (Object) exists



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

def exists
  if @tag = Tag.exists?(['?', params.require(:global_id)],
                        params.require(:keyword_id),
                        sessions_current_project_id)
    render :show
  else
    render json: false
  end
end

- (Object) index

GET /tags GET /tags.json



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/tags_controller.rb', line 8

def index
  respond_to do |format|
    format.html {
      @recent_objects = Tag.recent_from_project_id(sessions_current_project_id).order(updated_at: :desc).limit(10)
      render '/shared/data/all/index'
    }
    format.json {
      @tags = Queries::Tag::Filter.new(params).all.limit(500)
        .where(project_id: sessions_current_project_id)
    }
  end
end

- (Object) list



84
85
86
# File 'app/controllers/tags_controller.rb', line 84

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

- (Object) new



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/tags_controller.rb', line 21

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_object_id]}" \
                                    "&tag[tag_object_type]=#{params[: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



89
90
91
92
93
94
95
96
# File 'app/controllers/tags_controller.rb', line 89

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)



141
142
143
# File 'app/controllers/tags_controller.rb', line 141

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

- (Object) tag_object_update



32
33
34
35
# File 'app/controllers/tags_controller.rb', line 32

def tag_object_update
  taggable_object.update!(taggable_object_params)
  redirect_back(fallback_location: (request.referer || root_path))
end

- (Object) tag_params (private)



145
146
147
148
149
150
# File 'app/controllers/tags_controller.rb', line 145

def tag_params
  params.require(:tag).permit(
    :keyword_id, :tag_object_id, :tag_object_type, :tag_object_attribute, :annotated_global_entity, :_destroy,
    keyword_attributes: [:name, :definition, :uri, :uri_relation, :css_color]
  )
end

- (Object) taggable_object (private)



160
161
162
# File 'app/controllers/tags_controller.rb', line 160

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

- (Object) taggable_object_params (private)



152
153
154
155
156
157
158
# File 'app/controllers/tags_controller.rb', line 152

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



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/tags_controller.rb', line 58

def update
  respond_to do |format|
    if @tag.update(tag_params)
      format.html { redirect_to url_for(@tag.tag_object.metamorphosize),
                                notice: 'Tag was successfully updated.' }
      format.json { render :show, status: :ok, location: @tag }
    else
      format.html { redirect_back(fallback_location: (request.referer || root_path),
                                  notice: 'Tag was NOT successfully updated.') }
      format.json { render json: @tag.errors, status: :unprocessable_entity }
    end
  end
end