railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.89k stars 2.26k forks source link

Field order on csv export doesn't work #1042

Closed scottalankrieger closed 12 years ago

scottalankrieger commented 12 years ago

In previous versions of Rails admin you could control the field order in the csv export, now you don't seem to be able to unless I'm missing something. Here's my config:

config.model Order do
       configure :email, :string 
       configure :customer_type, :string 
       configure :first_name, :string 
       configure :last_name, :string 
       configure :company, :string 
       configure :street, :string 
       configure :city, :string 
       configure :state, :string 
       configure :zip, :string 
       configure :title, :string 
       configure :country, :string 
       configure :phone, :string 
       configure :customer_number, :integer 
       configure :shipping_account_number, :string 
       configure :comments, :text 
       configure :created_at, :datetime 
       configure :updated_at, :datetime 
       configure :ship_to_company, :string 
       configure :ship_to_street, :string 
       configure :ship_to_city, :string 
       configure :ship_to_state, :string 
       configure :ship_to_zip, :string 
       configure :ship_to_country, :string 
       configure :ship_to_contact_name, :string
       export do
           field :created_at
           field :first_name
           field :last_name
           field :email
           field :customer_type
           field :title
           field :phone
           field :street
           field :city
           field :state
           field :zip
           field :country
           field :company
           field :customer_number
           field :comments
           field :ship_to_street
           field :ship_to_city
           field :ship_to_state
           field :ship_to_zip
           field :ship_to_country
           field :ship_to_company
           field :shipping_account_number
      end
end      

Any ideas?

bbenezech commented 12 years ago

This looks good and should achieve want you want.

Maybe you are freezing fields order outside the scope of this excerpt.

Post your full initializer if you want, I'll tell you.

scottalankrieger commented 12 years ago

I appreciate it. Thanks. Here's what I have:

RailsAdmin.config do |config|

  config.current_user_method { current_administrator } # auto-generated

  config.main_app_name = ['GOM', 'Admin']

   config.model Order do
     configure :line_items, :has_many_association  
     configure :id, :integer 
     configure :email, :string 
     configure :customer_type, :string 
     configure :first_name, :string 
     configure :last_name, :string 
     configure :company, :string 
     configure :street, :string 
     configure :city, :string 
     configure :state, :string 
     configure :zip, :string 
     configure :title, :string 
     configure :country, :string 
     configure :phone, :string 
     configure :customer_number, :integer 
     configure :shipping_account_number, :string 
     configure :comments, :text 
     configure :created_at, :datetime 
     configure :updated_at, :datetime 
     configure :ship_to_company, :string 
     configure :ship_to_street, :string 
     configure :ship_to_city, :string 
     configure :ship_to_state, :string 
     configure :ship_to_zip, :string 
     configure :ship_to_country, :string 
     configure :ship_to_contact_name, :string   
     export do
           field :created_at
           field :first_name
           field :last_name
           field :email
           field :customer_type
           field :title
           field :phone
           field :street
           field :city
           field :state
           field :zip
           field :country
           field :company
           field :customer_number
           field :comments
           field :ship_to_street
           field :ship_to_city
           field :ship_to_state
           field :ship_to_zip
           field :ship_to_country
           field :ship_to_company
           field :shipping_account_number
           field :items
         end
   end      

config.model Pattern do

  field :sku, :string 
  field :name, :string
  field :featured, :boolean
  field :id, :integer 
  field :updated_at, :datetime 
  field :swatches, :has_many_association  
  field :status, :string 
  field :created_at, :datetime 
  field :param, :string 
  field :contents, :text 
  field :weight, :text 
  field :width, :text 
  field :repeat, :text 
  field :cleaning_code, :text 
  field :breaking_strength_fill, :text 
  field :breaking_strength_warp, :text 
  field :tear_fill, :string 
  field :tear_warp, :string 
  field :colorfastness_to_light, :text 
  field :colorfastness_to_crocking, :text 
  field :flammability, :text 
  field :misc, :text 
  field :terratex_and_carbon_neutral, :boolean 
  field :ul_testing, :boolean 
  field :wyzenbeek, :text 
  field :martindale, :text 
  field :seam_slippage, :text 
  field :pilling_resistance, :text 
  field :colorfastness_to_water, :text 
  field :treatment_text, :text 
  field :backing_text, :text 
  field :nrc_of_anechoic_termination, :string 
  field :nrc_of_fabric_in_front_of_anechoic_termination, :string 
  field :acoustic_chart, :paperclip 
  field :representative_featured_swatch do
    associated_collection_scope do
      pattern = bindings[:object]
      Proc.new { |scope|
        scope = scope.where( :pattern_id => pattern.id) if pattern.present?
        scope = scope.limit(200).order('name ASC')
      }
    end
  end
end

  config.model Swatch do
    list do
      field :name
      field :sku
      field :pattern
      field :id
    end
    edit do
      field :name, :string
      field :sku, :string
      field :pattern, :belongs_to_association 
      field :status, :belongs_to_association 
      field :categories, :has_and_belongs_to_many_association 
      field :markets, :has_and_belongs_to_many_association 
      field :featured, :boolean
      field :priority, :integer
      field :sites, :has_and_belongs_to_many_association   #   # Found columns:
      field :id, :integer 
      field :image, :paperclip 
      field :color, :string 
    end
  end  

  config.model User do
      configure :id, :integer 
      configure :email, :string 
      configure :remember_created_at, :datetime 
      configure :sign_in_count, :integer 
      configure :current_sign_in_at, :datetime 
      configure :last_sign_in_at, :datetime 
      configure :current_sign_in_ip, :string 
      configure :last_sign_in_ip, :string 
      configure :created_at, :datetime 
      configure :updated_at, :datetime 
      configure :customer_type, :string 
      configure :first_name, :string 
      configure :last_name, :string 
      configure :company, :string 
      configure :street, :string 
      configure :city, :string 
      configure :state, :string 
      configure :zip, :string 
      configure :title, :string 
      configure :country, :string 
      configure :phone, :string 
      configure :customer_number, :string 
      configure :shipping_account_number, :string 
      configure :comments, :string 
      configure :cart, :text 
      configure :site, :integer  
  end
end
bbenezech commented 12 years ago

It looks good! You should have default order for all actions except for export where it should be [:created_at, :first_name, etc;].

What order do you have for export?

scottalankrieger commented 12 years ago

Export comes through in the order of the fields in the database table.

bbenezech commented 12 years ago

Dammit, found the pb.

You nailed a huge design flaw, actually...

fields verb is used internally for sections' inheritance and is fixing the ordering of the fields... because it can also be used in the DSL. And right now, you can't change the ordering of inherited section's fields.

config.model Team
  fields :name, :id   # setting #defined to true and fixing ordering
end

Pb is both are 'API-certified', so I can't modified any of those 2 behaviour...

We should not have used the same verb for reading and writting :/

I need to think a bit. Maybe solving the 'we can't change ordering' part would solve this.

bbenezech commented 12 years ago

Found the REAL issue on your specific problem (it was a dumb CSV exporter bug) Now for the design flaw, you still can't change inherited field ordering. I'll see to that some day.

natanr123 commented 5 years ago

I have fixed it by change initialize function in /lib/rails_admin/support/csv_converter.rb in the gem code But it broke stuff where I did not explicitly described the export fields: Notice the override of the @method variable

def initialize(objects = [], schema = {}) @fields = [] @associations = [] return self if (@objects = objects).blank? @model = objects.dup.first.class @abstract_model = RailsAdmin::AbstractModel.new(@model) @model_config = @abstract_model.config

@methods = [(schema[:only] || []) + (schema[:methods] || [])].flatten.compact

    @methods = @model_config.export.visible_fields.collect { |f| f.name }