Skip to content

Delete empty translation records on save #10

Closed
@codingluke

Description

@codingluke

Hi guys, first I want to say THX for this gem! It saved me ages, is good designed and a pleasure to work with.

Today I came up with something strange, which maybe also is interesting for you. There are also records created for empty languages (all fields empty). As I want to give the users the ability to switch languages (if there are any other available) its a hassle to find out, which languages are filled and which not.

So I implemented an easy solution. A before_filter which removes empty languages form the params hash, or marks empty languages which existed (has han id) with the flag '_destroy' => '1'. Where as the first part works greate. The second part with the '_destory' => '1' gets ignored. I don't know why, because you set allow_destroy: true

# activeadmin-globalize3 / lib / active_admin / globalize3 / active_record_extension.rb
attr_accessible :translations_attributes
accepts_nested_attributes_for :translations, allow_destroy: true 

Here my code:

# Inside an own active_admin page
ActiveAdmin.register MyModel do
  before_filter :remove_or_mark_empty_translations, :only => [:update, :create]

  controller do
    private
    def remove_or_mark_empty_translations
      params[:requisition][:translations_attributes].each do |t|
        # iterates through the fields and checks if they are empty
        # the first two entries (id and locale) are ignored
        if !(t.last.map { |k,v| v.empty? ? true : false }[2..-1]).include?(false)
          if t.last[:id].empty?
            # If id is also empty, remove from params
            # works fine!
            params[:requisition][:translations_attributes].delete(t.first)
          else
            # otherwise mark to destroy
            # FIXME : seems to be ignored
            params[:requisition][:translations_attributes][t.first]['_destroy'] = '1'
          end
        end
      end
    end
  end
end

Have you any ideas or better solutions?

PS: to remove empty translation rows improves also the view helper translation_status ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions