README.md in smart_attr-0.1.1 vs README.md in smart_attr-0.2.0
- old
+ new
@@ -77,49 +77,59 @@
### Used With ActiveRecord
It is almost the same like the basic usage when used with ActiveRecord.
-The only difference is that it will not create an instance_variable to store the value when you set value for the attribute.
+There is just one difference between this and the basic usage, however, there is one extra functionality when used with ActiveRecord.
+
+The only one difference is that it will not create an instance_variable to store the value when you set value for the attribute.
This is because it will store the value in the database.
+And the extra functionality is that it will define scope for you when used with ActiveRecord.
For example, suppose you have a class named "Movie" with database table 'movies', then you should ensure that 'movies' have column 'star' before you use 'smart_attr :star, config: { # something }'
```ruby
-class Movie
+class Song < ActiveRecord::Base
include SmartAttr::Base
smart_attr :star, config: {
one: { value: 1, desc: 'one star' },
two: { value: 2, desc: 'two star' },
three: { value: 3, desc: 'three star' },
four: { value: 4, desc: 'four star' },
five: { value: 5, desc: 'five star' }
}
+
end
-Movie.star_config_hash
-# => { :one=>{:value=>1, :desc=>"one star"},
-# :two=>{:value=>2, :desc=>"two star"},
-# :three=>{:value=>3, :desc=>"three star"},
-# :four=>{:value=>4, :desc=>"four star"},
-# :five=>{:value=>5, :desc=>"five star"}
-# }
-movie = Movie.new # => #<Movie:0x007fcc041b0490>
+song = Song.new(star: 0)
-movie.star = 1 # => 1
-movie.star_name # => :one
-movie.star_desc # => "one star"
-movie.star_one? # => true
-movie.star_two? # => false
+song.save
-movie.star_two! # => 2
-movie.star # => 2
-movie.star_two? # => true
+song.star = 1 # => 1
+song.star_name # => :one
+song.star_desc # => "one star"
+song.star_one? # => true
+song.star_two? # => false
-movie.star_config # => {:value=>2, :desc=>"two star", :key=>:two}
+song.star_two! # => 2
+song.star # => 2
+song.star_two? # => true
+
+song.reload
+song.star # => 1
+song.star_two!
+song.save
+song.reload
+song.star # => 2
+
+song.star_config # => {:value=>2, :desc=>"two star", :key=>:two}
+
+# scope
+Song.star_one # The same as: Song.where(star: 1)
+Song.star_two # The same as: Song.where(star: 2)
```
### Used With Mongoid
Same like used with ActiveRecord.