upmin / upmin-admin-ruby

Framework for creating powerful admin backends with minimal effort in Ruby on Rails.
MIT License
754 stars 66 forks source link

Populates model objects with incorrect attribute for specific field 'title' #85

Open ColinTheRobot opened 10 years ago

ColinTheRobot commented 10 years ago

The Issue

Form in model view as well as in the initial view of all objects related to a specific model, pulls the wrong value for a single attribute for one specific model.

It only appears to happen with this one specific field name ( t.string "title" ).

Versions and gemfile

Upmin 0.1.0 Rails 4.1.4 Ruby 2.0.0p451

source 'https://rubygems.org'

ruby '2.0.0'

gem 'rails', '4.1.4'

# Core
gem 'pg'
gem 'unicorn'
gem 'airbrake'
gem 'redis'
gem 'resque', "~> 1.22.0"
gem 'dotenv-rails'

# API
gem 'active_model_serializers'
gem 'apipie-rails', git: 'git://github.com/Pajk/apipie-rails'

# Auth
gem 'devise'
gem 'cancan'

# Admin
gem 'upmin-admin'

# Attachments/File Uploads
# gem 'paperclip', github: 'thoughtbot/paperclip'
# gem 'aws-sdk'

group :assets do
  gem 'sass'
  gem 'bootstrap-sass', '~> 3.2.0'
  gem 'sass-rails', '~> 4.0.2' #, github: 'rails/sass-rails'
  gem 'font-awesome-rails'
  gem 'uglifier'
end

group :production do
  gem 'heroku-deflater'
  gem 'rails_12factor'
end

group :development do
  gem 'spring'
end

group :development, :test do
  gem 'guard-bundler'
  gem 'guard-rspec'
  gem 'rspec-rails'
  gem 'rspec-collection_matchers'
  gem 'factory_girl_rails'
  gem 'faker'
end

group :development, :test, :integration, :staging do
  gem 'byebug'
  gem 'pry-rails', '~> 0.3.2'
end

group :test do
  gem 'webmock'
  gem 'spork', '> 0.9.0.rc'
  gem 'guard-spork'
end

model attributes and sample object

The model in question has the following attributes:

  create_table "topics", force: true do |t|
    t.string   "title"
    t.integer  "user_id"
    t.string   "cover_image"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
#<Topic id: 2, title: "Function-based needs-based projection", user_id: nil, cover_image: "http://robohash.org/eosliberoautem.png?size=300x30...", created_at: "2014-10-09 16:06:51", updated_at: "2014-10-09 16:06:51"> 

From the outset, the Topics view does not show the correct title, "Function-based needs-based projection", but shows Title Topic # 2

screen shot 2014-10-09 at 5 50 47 pm

Similarly, after clicking the Topic object, Title in the form form#edit_topic_2 continues to read as Topic # 2 as does model.title called in the <h3> tag at the top.

I did notice that specifying model.model.title displays the correct title.

Further functionality I explored was a custom admin action for topics to update the title.

_app/upmin/models/admintopics.rb

class AdminTopic < Upmin::Model
  attributes :id, :title, :cover_image
  actions :update_title

  def update_title(new_title)
    model.new_title = new_title
    model.save!
  end
end

This action does render correctly on the page, and performs the action as expected—updating the Topic Title attribute. However, while the change is reflected in the database, it is not reflected in the model view.

joncalhoun commented 10 years ago

Thanks for all of the info.

The fix is basically chanigng https://github.com/upmin/upmin-admin-ruby/blob/master/lib/upmin/attribute.rb#L13 to call model.model.send (instead of model.send) for any attribute name equal to the instance methods in Upmin::Model https://github.com/upmin/upmin-admin-ruby/blob/master/lib/upmin/model.rb

The full (current) list would be:

Until this change gets committed, I would add the following in your app to app/views/upmin/partials/attributes/_topic_title.html.haml:

- title_value = attribute.model.model.title
- is_nil = model.new_record? ? false : title_value.nil?

.form-group{class: attribute.errors? ? "has-error" : ""}
  %label{for: attribute.form_id}
    = attribute.label_name

  - if attribute.editable? && f = form_builder
    .input-group
      = f.text_field(attribute.name, value: title_value, class: "form-control")
      .input-group-addon.nilable-addon
        .form-group
          %label{for: "#{attribute.form_id}_is_nil"}
            Make this Nil
          = check_box(model.underscore_name, "#{attribute.name}_is_nil", class: "boolean", checked: is_nil, value: is_nil)

  - else
    %p.well
      = title_value

This won't fix the search results, but it will fix the the form view temporarily.

Just FYI - I haven't tested the code above, so if it doesn't work first try let me know and I'll help you debug it, but that should be correct.

ColinTheRobot commented 10 years ago

That's great thanks! It totally works.

raels commented 10 years ago

Is there an edge branch that has the model.model.send fix in it?