From be56f7685b56fbf8302af01684f25d5f1c9bb25d Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Wed, 18 Jun 2014 12:09:19 +0800 Subject: [PATCH] fixed delete and author --- .../admin/journal_papers_controller.rb | 6 +++--- app/models/journal_paper.rb | 18 ++++++++++++++++++ app/models/journal_paper_author.rb | 6 ++---- app/models/journal_paper_file.rb | 1 + app/views/admin/journal_papers/_form.html.erb | 5 +---- .../admin/journal_papers/_form_author.html.erb | 2 +- .../admin/journal_papers/_form_file.html.erb | 2 +- config/locales/en.yml | 2 +- 8 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/controllers/admin/journal_papers_controller.rb b/app/controllers/admin/journal_papers_controller.rb index c028c38..0c814f4 100644 --- a/app/controllers/admin/journal_papers_controller.rb +++ b/app/controllers/admin/journal_papers_controller.rb @@ -2,7 +2,7 @@ class Admin::JournalPapersController < OrbitMemberController before_action :set_journal_paper, only: [:show, :edit , :update, :destroy] before_action :get_plugins, only: [:index, :writing_journal_setting, :new, :create, :edit, :update] - before_action :set_types, only: [:writing_journal_setting, :new, :edit, :create] + before_action :set_types, only: [:writing_journal_setting, :new, :edit, :create, :update] def index @writing_journals = JournalPaper.all @@ -23,7 +23,7 @@ class Admin::JournalPapersController < OrbitMemberController def new @journal_paper = JournalPaper.new - @members_data = JournalPaper.member_data + @members_data = JournalPaper.member_data rescue nil respond_to do |format| format.html # new.html.erb @@ -33,7 +33,7 @@ class Admin::JournalPapersController < OrbitMemberController def edit @journal_paper = JournalPaper.find(params[:id]) - @members_data = JournalPaper.member_data + @members_data = JournalPaper.member_data rescue nil end def create diff --git a/app/models/journal_paper.rb b/app/models/journal_paper.rb index bb9b245..3226317 100644 --- a/app/models/journal_paper.rb +++ b/app/models/journal_paper.rb @@ -34,6 +34,8 @@ class JournalPaper has_and_belongs_to_many :journal_paper_authors accepts_nested_attributes_for :journal_paper_authors + after_save :save_journal_paper_files, :save_journal_paper_authors + before_validation :add_http validates :paper_title, presence: true @@ -71,6 +73,22 @@ class JournalPaper member_data.to_json end + def save_journal_paper_files + self.journal_paper_files.each do |t| + if t.should_destroy + t.destroy + end + end + end + + def save_journal_paper_authors + self.journal_paper_authors.each do |t| + if t.should_destroy + t.destroy + end + end + end + protected def add_http diff --git a/app/models/journal_paper_author.rb b/app/models/journal_paper_author.rb index 2a3a880..9de194b 100644 --- a/app/models/journal_paper_author.rb +++ b/app/models/journal_paper_author.rb @@ -4,14 +4,12 @@ class JournalPaperAuthor field :name, type: String, localize: true field :email, type: String + field :should_destroy, type: Boolean has_and_belongs_to_many :journal_papers has_and_belongs_to_many :journal_paper_author_types VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/ validates :email, format: { with: VALID_EMAIL_REGEX }, - allow_blank: true, - uniqueness: { case_sensitive: false } - - before_save { |author| author.email = email.downcase if not author.email.nil?} + allow_blank: true end diff --git a/app/models/journal_paper_file.rb b/app/models/journal_paper_file.rb index e6040a1..36af09c 100644 --- a/app/models/journal_paper_file.rb +++ b/app/models/journal_paper_file.rb @@ -5,6 +5,7 @@ class JournalPaperFile field :description, type: String, localize: true field :title, type: String, localize: true + field :should_destroy, type: Boolean mount_uploader :journal_file, AssetUploader belongs_to :journal_paper diff --git a/app/views/admin/journal_papers/_form.html.erb b/app/views/admin/journal_papers/_form.html.erb index 40bbaa9..fc4bd98 100644 --- a/app/views/admin/journal_papers/_form.html.erb +++ b/app/views/admin/journal_papers/_form.html.erb @@ -24,11 +24,8 @@
  • <%= t(:basic) %>
  • -
  • - <%= t(:status) %> -
  • - <%= t(:tags) %> + <%= t(:status) %>
  • diff --git a/app/views/admin/journal_papers/_form_author.html.erb b/app/views/admin/journal_papers/_form_author.html.erb index 8ccef3c..0d6826d 100644 --- a/app/views/admin/journal_papers/_form_author.html.erb +++ b/app/views/admin/journal_papers/_form_author.html.erb @@ -32,7 +32,7 @@ <%= f.hidden_field :id %> - <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + <%= f.hidden_field :should_destroy, :value => nil, :class => 'should_destroy' %> <% end %> diff --git a/app/views/admin/journal_papers/_form_file.html.erb b/app/views/admin/journal_papers/_form_file.html.erb index b8f6fed..0a3aaab 100644 --- a/app/views/admin/journal_papers/_form_file.html.erb +++ b/app/views/admin/journal_papers/_form_file.html.erb @@ -38,7 +38,7 @@ <%= f.hidden_field :id %> - <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + <%= f.hidden_field :should_destroy, :value => nil, :class => 'should_destroy' %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index cb01bcb..acf8ed5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -46,7 +46,7 @@ en: yes_: "Yes" no_: "No" cancel : "Cancel" - save: "save" + save: "Save" hintText: "Type in a search term" noResultsText: "No results" searchingText: "Searching…" \ No newline at end of file