app/controllers/data_objects_controller.rb
# POST /pages/:taxon_id/data_objects
# We're only creating new user data objects in the context of a taxon concept so we need taxon_id to be provided in route
POST /pages/:taxon_id/data_objects
We're only creating new user data objects in the context of a taxon concept so we need taxon_id to be provided in route
POST / pages / : taxon_id / data_objects We ' re only creating new user data objects in the context of a taxon concept so we need taxon_id to be provided in route
def create
    @taxon_concept = TaxonConcept.find(params[:taxon_id])
    unless params[:data_object] && params[:data_object][:data_type_id].to_i == DataType.text.id
      create_failed
      return
    end
    @references = params[:references] 
    raise I18n.t(:dato_create_user_text_missing_user_exception) if current_user.nil?
    raise I18n.t(:dato_create_user_text_missing_taxon_id_exception) if @taxon_concept.blank?
    if DataObject.same_as_last?(params, user: current_user,taxon_concept: @taxon_concept )
      flash[:notice] = I18n.t(:duplicate_text_warning)
      self.new && return
    elsif DataObject.spammy?(params, current_user)
      flash[:notice] = I18n.t(:error_violates_tos)
      self.new && return
    end
    @data_object = DataObject.create_user_text(
      params[:data_object],
      user: current_user,
      taxon_concept: @taxon_concept,
      toc_id: toc_id,
      object_created_at: Time.now,
      object_modified_at: Time.now,
      link_type_id: link_type_id,
      link_object: params[:commit_link]
    )
    if @data_object.nil? || @data_object.errors.any?
      @selected_toc_item_id = toc_id
      create_failed && return
    else
      @taxon_concept.reload 
      add_references(@data_object)
      collection_item = CollectionItem.create(
        collected_item: @data_object,
        collection: current_user.watch_collection
      )
      CollectionActivityLog.create(collection: current_user.watch_collection, user_id: current_user.id,
                                   activity: Activity.collect, collection_item: collection_item)
      @data_object.log_activity_in_solr(keyword: 'create', user: current_user, taxon_concept: @taxon_concept)
      if @data_object.is_link?
        case @data_object.link_type.id
        when LinkType.blog.id
          redirect_path = news_and_event_links_taxon_resources_url(@taxon_concept, anchor: "data_object_#{@data_object.id}")
        when LinkType.news.id
          redirect_path = news_and_event_links_taxon_resources_url(@taxon_concept, anchor: "data_object_#{@data_object.id}")
        when LinkType.organization.id
          redirect_path = related_organizations_taxon_resources_url(@taxon_concept, anchor: "data_object_#{@data_object.id}")
        when LinkType.paper.id
          redirect_path = literature_links_taxon_literature_url(@taxon_concept, anchor: "data_object_#{@data_object.id}")
        when LinkType.multimedia.id
          redirect_path = multimedia_links_taxon_resources_url(@taxon_concept, anchor: "data_object_#{@data_object.id}")
        else
          redirect_path = taxon_details_path(@taxon_concept, anchor: "data_object_#{@data_object.id}")
        end
        return redirect_to redirect_path, status: :moved_permanently
      end
      subchapter = nil
      DataObject.with_master do
        subchapter = @data_object.toc_items.first.label.downcase
      end
      subchapter = 'literature' if subchapter == 'literature references'
      subchapter.gsub!(/ /, "_" )
      temp = ["education", "education_resources", "identification_resources", "nucleotide_sequences",
        "biomedical_terms", "citizen_science_links"] 
      if temp.include?(subchapter)
        return redirect_to education_taxon_resources_path(@taxon_concept,
                             anchor: "data_object_#{@data_object.id}"), status: :moved_permanently if
          ['education', 'education_resources'].include?(subchapter)
        return redirect_to identification_resources_taxon_resources_path(@taxon_concept,
                             anchor: "data_object_#{@data_object.id}"), status: :moved_permanently if
          subchapter == 'identification_resources'
        return redirect_to nucleotide_sequences_taxon_resources_path(@taxon_concept,
                             anchor: "data_object_#{@data_object.id}"), status: :moved_permanently if
          subchapter == 'nucleotide_sequences'
        return redirect_to biomedical_terms_taxon_resources_path(@taxon_concept,
                             anchor: "data_object_#{@data_object.id}"), status: :moved_permanently if
          subchapter == 'biomedical_terms'
        return redirect_to citizen_science_taxon_resources_path(@taxon_concept,
                             anchor: "data_object_#{@data_object.id}"), status: :moved_permanently if
          subchapter == 'citizen_science_links'
      elsif ["literature"].include?(subchapter)
        return redirect_to literature_taxon_literature_path(@taxon_concept,
                             anchor: "data_object_#{@data_object.id}"), status: :moved_permanently if
          subchapter == 'literature'
      end
      return redirect_to taxon_details_path(@taxon_concept, anchor: "data_object_#{@data_object.id}"), status: :moved_permanently
    end
  end
def create @taxon_concept = TaxonConcept . find ( params [ :taxon_id ] ) unless params [ :data_object ] && params [ :data_object ] [ :data_type_id ] . to_i == DataType . text . id create_failed return end @references = params [ :references ] raise I18n . t ( :dato_create_user_text_missing_user_exception ) if current_user . nil? raise I18n . t ( :dato_create_user_text_missing_taxon_id_exception ) if @taxon_concept . blank? if DataObject . same_as_last? ( params , user : current_user , taxon_concept : @taxon_concept ) flash [ :notice ] = I18n . t ( :duplicate_text_warning ) self . new && return elsif DataObject . spammy? ( params , current_user ) flash [ :notice ] = I18n . t ( :error_violates_tos ) self . new && return end @data_object = DataObject . create_user_text ( params [ :data_object ] , user : current_user , taxon_concept : @taxon_concept , toc_id : toc_id , object_created_at : Time . now , object_modified_at : Time . now , link_type_id : link_type_id , link_object : params [ :commit_link ] ) if @data_object . nil? || @data_object . errors . any? @selected_toc_item_id = toc_id create_failed && return else @taxon_concept . reload add_references ( @data_object ) collection_item = CollectionItem . create ( collected_item : @data_object , collection : current_user . watch_collection ) CollectionActivityLog . create ( collection : current_user . watch_collection , user_id : current_user . id , activity : Activity . collect , collection_item : collection_item ) @data_object . log_activity_in_solr ( keyword : 'create' , user : current_user , taxon_concept : @taxon_concept ) if @data_object . is_link? case @data_object . link_type . id when LinkType . blog . id redirect_path = news_and_event_links_taxon_resources_url ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) when LinkType . news . id redirect_path = news_and_event_links_taxon_resources_url ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) when LinkType . organization . id redirect_path = related_organizations_taxon_resources_url ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) when LinkType . paper . id redirect_path = literature_links_taxon_literature_url ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) when LinkType . multimedia . id redirect_path = multimedia_links_taxon_resources_url ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) else redirect_path = taxon_details_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) end return redirect_to redirect_path , status : :moved_permanently end subchapter = nil DataObject . with_master do subchapter = @data_object . toc_items . first . label . downcase end subchapter = 'literature' if subchapter == 'literature references' subchapter . gsub! ( / / , "_" ) temp = [ "education" , "education_resources" , "identification_resources" , "nucleotide_sequences" , "biomedical_terms" , "citizen_science_links" ] if temp . include? ( subchapter ) return redirect_to education_taxon_resources_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently if [ 'education' , 'education_resources' ] . include? ( subchapter ) return redirect_to identification_resources_taxon_resources_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently if subchapter == 'identification_resources' return redirect_to nucleotide_sequences_taxon_resources_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently if subchapter == 'nucleotide_sequences' return redirect_to biomedical_terms_taxon_resources_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently if subchapter == 'biomedical_terms' return redirect_to citizen_science_taxon_resources_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently if subchapter == 'citizen_science_links' elsif [ "literature" ] . include? ( subchapter ) return redirect_to literature_taxon_literature_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently if subchapter == 'literature' end return redirect_to taxon_details_path ( @taxon_concept , anchor : "data_object_#{@data_object.id}" ) , status : :moved_permanently end end
POST /pages/:taxon_id/data_objects
We're only creating new user data objects in the context of a taxon concept so we need taxon_id to be provided in route
POST / pages / : taxon_id / data_objects We ' re only creating new user data objects in the context of a taxon concept so we need taxon_id to be provided in route
# we'll need these if validation fails and we re-render new # Clears caches, too! # add this new object to the user's watch collection # redirect to appropriate tab/sub-tab after creating the users_data_object/link_object # TODO - this case definitely smells like it should be duck-typed. # Will try to redirect to the appropriate tab/section after adding text # Make sure they have the latest version of toc items: # to Resources tab