huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

Add attr_accessible if protected-attributes gem is detected #64

Closed adibsaad closed 9 years ago

adibsaad commented 9 years ago

As per my comment in #37 , it turns out my project was using the protected attributes gem and it was a Rails 4.2.1 app (sorry for not noticing). I simply added a check to see if the gem is being used.

adibsaad commented 9 years ago

@huacnlee The CI keeps failing for reasons beyond my control, but this PR is really needed for projects that are either partially migrated or trying to be migrated from Rails 3 to 4.

huacnlee commented 9 years ago
  1. Use protected_attributes to try to fix yours problem that is not the right way.
  2. Why do you need that? I have test #45 case by use Rails 4.2, there is no protected attribute error (See my latest reply in that issue)
adibsaad commented 9 years ago

@huacnlee Yes but for the test case in #45, did you install the protected-attributes in that rails project?

huacnlee commented 9 years ago

No, we don't need that gem. and so?

adibsaad commented 9 years ago

@huacnlee Yes, but what I'm saying is, what do we do for people who are using your gem in their Rails 4 project while using the protected-attributes gem?

huacnlee commented 9 years ago
~/work/foo> cat Gemfile.lock | grep protected_attributes -A 2
    protected_attributes (1.1.3)
      activemodel (>= 4.0.1, < 5.0)
    rack (1.6.4)
--
  protected_attributes
  rails (= 4.2.4)
~/work/foo> cat Gemfile.lock | grep rails-settings-cached
    rails-settings-cached (0.4.2)
~/work/foo> cat app/models/user.rb    
class User < ActiveRecord::Base
  include RailsSettings::Extend
end

~/work/foo> rails c
Loading development environment (Rails 4.2.4)
2.2.2 :001 > Setting.foo = "bar"
  Setting Load (0.5ms)  SELECT  "settings".* FROM "settings" WHERE (thing_type is NULL and thing_id is NULL) AND "settings"."var" = ?  ORDER BY "settings"."id" ASC LIMIT 1  [["var", "foo"]]
   (0.1ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "settings" ("var", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["var", "foo"], ["value", "--- bar\n...\n"], ["created_at", "2015-09-10 03:44:56.972226"], ["updated_at", "2015-09-10 03:44:56.972226"]]
   (0.7ms)  commit transaction
 => "bar" 
2.2.2 :002 > Setting.foo
 => "bar" 
2.2.2 :001 > u = User.create(name: 'huacnlee')
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "users" ("name", "created_at", "updated_at") VALUES (?, ?, ?)  [["name", "huacnlee"], ["created_at", "2015-09-10 03:43:22.196003"], ["updated_at", "2015-09-10 03:43:22.196003"]]
   (0.7ms)  commit transaction
 => #<User id: 1, name: "huacnlee", created_at: "2015-09-10 03:43:22", updated_at: "2015-09-10 03:43:22"> 
2.2.2 :002 > u.settings
 => RailsSettings::ScopedSettings(id: integer, var: string, value: text, thing_id: integer, thing_type: string, created_at: datetime, updated_at: datetime) 
2.2.2 :003 > u.settings.enable_color = true
  RailsSettings::ScopedSettings Load (0.2ms)  SELECT  "settings".* FROM "settings" WHERE "settings"."thing_type" = ? AND "settings"."thing_id" = ? AND "settings"."var" = ?  ORDER BY "settings"."id" ASC LIMIT 1  [["thing_type", "User"], ["thing_id", 1], ["var", "enable_color"]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "settings" ("thing_type", "thing_id", "var", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["thing_type", "User"], ["thing_id", 1], ["var", "enable_color"], ["value", "--- true\n...\n"], ["created_at", "2015-09-10 03:43:31.998516"], ["updated_at", "2015-09-10 03:43:31.998516"]]
   (0.8ms)  commit transaction
 => true 
2.2.2 :004 > u.settings.enable_email_notify = true
  RailsSettings::ScopedSettings Load (0.2ms)  SELECT  "settings".* FROM "settings" WHERE "settings"."thing_type" = ? AND "settings"."thing_id" = ? AND "settings"."var" = ?  ORDER BY "settings"."id" ASC LIMIT 1  [["thing_type", "User"], ["thing_id", 1], ["var", "enable_email_notify"]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "settings" ("thing_type", "thing_id", "var", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["thing_type", "User"], ["thing_id", 1], ["var", "enable_email_notify"], ["value", "--- true\n...\n"], ["created_at", "2015-09-10 03:43:38.637599"], ["updated_at", "2015-09-10 03:43:38.637599"]]
   (2.5ms)  commit transaction
 => true 
2.2.2 :005 > u.settings
 => RailsSettings::ScopedSettings(id: integer, var: string, value: text, thing_id: integer, thing_type: string, created_at: datetime, updated_at: datetime) 
2.2.2 :006 > u.settings.get_all
  RailsSettings::ScopedSettings Load (0.3ms)  SELECT var, value FROM "settings" WHERE "settings"."thing_type" = ? AND "settings"."thing_id" = ?  [["thing_type", "User"], ["thing_id", 1]]
 => {"enable_color"=>true, "enable_email_notify"=>true}