Comments (5)
is User.new.email_preferences == 0 ?
if not, change_column :users, :email_preferences, :integer, :default => 0, :null => false
from bitfields.
Thanks for responding @grosser.
Setting the db column default to 0 allows the above spec to pass, but it causes this spec to fail:
describe "set_email_prefs_to_defaults" do
it "should set the email preferences to the defaults on create" do
user = Factory(:user)
user.credits_email.should == true
user.weekly_results_email.should == true
user.challenges_email.should == true
user.jackpot_results_email.should == true
end
end
because attribute_present?(:email_preferences)
in my previous set_email_prefs_to_defaults
method returns true
due to the db column default.
I did, however, find a nice solution that allows both specs to pass:
class User < ActiveRecord::Base
...
after_initialize :set_email_prefs_to_defaults
...
def set_email_prefs_to_defaults
self.credits_email ||= true
self.weekly_results_email ||= true
self.challenges_email ||= true
self.jackpot_results_email ||= true
end
end
from bitfields.
setting bitfield columns before create / initialize is a complicated topic,
but i think you almost nailed it, try before_initialize, then even
User.new(:credits_email=>false).credits_email.should == false
from bitfields.
There is no before_initialize ...
I added a spec for how it should work, but so far its failing, maybe you can find a way to fix it :)
2170dc5
from bitfields.
closing this, doing false ||= true
is true ... no bug here ...
from bitfields.
Related Issues (16)
- Bitfields don't show up when serializing the model. HOT 3
- Rails 4.2 compatibility HOT 3
- bitfield column (e.g., flags) missing during tests HOT 2
- Never using bit0 HOT 3
- NoMethodError: undefined method `failure_message' for #<RSpec::Matchers::DSL::Matcher> HOT 1
- Impossible to have multiple bitfields columns HOT 8
- Get value for hash of bitfields HOT 2
- Deprecation Warnings in AR 5.1/Likely breaking in AR 5.2 HOT 1
- Feature request: Inverted bit fields HOT 2
- Volunteer to improve HOT 3
- Why you say that is not possible to configure defaults? HOT 2
- Requires AsMut and AsRef with buffers > 32 bytes HOT 2
- Querying with joins HOT 1
- Querying with #includes HOT 1
- Allow bitfield_sql to use ORs instead of ANDs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bitfields.