Class: TopicsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/topics_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

#polymorphic_filter_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

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) autocomplete

TODO: This format very different, should it be made same as other autocompletes?



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/topics_controller.rb', line 65

def autocomplete
  topics = Topic.find_for_autocomplete(params.merge(project_id: sessions_current_project_id))

  data = topics.collect do |t|
    str = t.name + ": " + t.definition
    {id:              t.id,
     label:           str,
     response_values: {
       params[:method] => t.id},
     label_html:      str
    }
  end

  render :json => data
end

- (Object) create

POST /controlled_vocabulary_terms POST /controlled_vocabulary_terms.json



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/topics_controller.rb', line 30

def create
  @controlled_vocabulary_term = ControlledVocabularyTerm.new(controlled_vocabulary_term_params)
  respond_to do |format|
    if @controlled_vocabulary_term.save
      msg = "#{@controlled_vocabulary_term.type} '#{@controlled_vocabulary_term.name}' was successfully created."

      if !params.permit(:return_path)[:return_path].blank?
        # case - coming from otu -> new tag -> new cvt -> back to tag/new
        redirect_url = params.permit(:return_path)[:return_path] + "&tag[keyword_id]=#{@controlled_vocabulary_term.to_param}"
      elsif request.env['HTTP_REFERER'].include?('controlled_vocabulary_terms/new')
        # case - coming from cvt index -> cvt/new
        redirect_url = controlled_vocabulary_term_path(@controlled_vocabulary_term)
      else
        # case - coming from task -> return to task
        redirect_url = :back
      end

      format.html { redirect_to redirect_url, notice: msg } # !! new behaviour to test
      format.json { render action: 'show', status: :created, location: @controlled_vocabulary_term.metamorphosize }

    else
      format.html {
        flash[:notice] = 'Controlled vocabulary term NOT successfully created.'
        if redirect_url == :back
          redirect_back(fallback_location: (request.referer || root_path), notice: 'Controlled vocabulary term NOT successfully created.')
        else
          render action: 'new'
        end
      }
      format.json { render json: @controlled_vocabulary_term.errors, status: :unprocessable_entity }
    end
  end
end

- (Object) get_definition



92
93
94
95
# File 'app/controllers/topics_controller.rb', line 92

def get_definition
  @topic = Topic.find(params[:id])
  render(:json => {definition: @topic.definition})
end

- (Object) index

get /topics.json



5
6
7
8
# File 'app/controllers/topics_controller.rb', line 5

def index
  @controlled_vocabulary_terms = Topic.order(:name).where(project_id: sessions_current_project_id).all
  render '/controlled_vocabulary_terms/index'
end

- (Object) list

TODO: deprecate for index? List should be paginated.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/topics_controller.rb', line 11

def list
  topics = Topic.order(:name).where(project_id: sessions_current_project_id).all

  data = topics.collect do |t|
    str = t.name + ": " + t.definition
    { id: t.id,
      name: t.name,
      definition: t.definition, 
      color: t.css_color,
      css_color: t.css_color,
      label:  t.name # TODO: referenced in js picker, refactor to use name rather than label
    }
  end

  render :json => data
end

- (Object) lookup_topic



81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/topics_controller.rb', line 81

def lookup_topic
  @topics = Topic.find_for_autocomplete(params.merge(project_id: sessions_current_project_id))
  render(:json => @topics.collect { |t|
    {
      label:     t.name,
      object_id: t.id,
      definition: t.definition
    }
  })
end