jasl / activeentity Goto Github PK
View Code? Open in Web Editor NEWActive Record without Database
License: Other
Active Record without Database
License: Other
I don't know if this is an issue, or possibly just an opportunity to create more documentation for the enum
module.
While I think I have come to understand the Active Entity enum implementation, my experiments suggest that its behaviour is different enough from ActiveRecord::Enum
that it's almost harmful to use the AR documentation for this feature.
Unlike AR enum
, AE enum
only accepts one parameter, and that parameter must be a Hash. This makes most of the examples at the top of https://github.com/jasl/activeentity/blob/master/lib/active_entity/enum.rb incorrect; for example, there are no scopes created and there is no concept of where
querying with AE. It supports suffixes and prefixes, but no other options.
AR enum
is described as "an enum attribute where the values map to integers in the database, but can be queried by name" but that is just not how the AE implementation works.
class Example < ActiveEntity::Base
attribute :status, :integer, default: 0
enum status: [:active, :inactive]
end
example = Example.new
example.attributes # => {"status"=>"active"}
Really, what we have is not an Integer, but a String that can be set with an Integer. I don't know if AE tracks the type cast but the important thing is that if I go to work with the attributes
it shows up as a String... meaning that logically, I should define status
as a String attribute when using Active Entity. However, that doesn't end well:
class Example < ActiveEntity::Base
attribute :status, :string, default: 10
enum status: {active: 10, inactive: 20}
end
example = Example.new
example.attributes # => {"status"=>"active"}
example.inactive! # => {"status"=>nil} <- I'm cheating a bit because AllFutures supports update!/save!
example.status = 20 # => {"status"=>"inactive"}
example.status = :active # => {"status"=>"active"}
So, to recap: AE enum
looks like and behaves as a String, but must be declared with Integer type or else the dynamic methods are not created properly.
I actually think that this is fine now that I have mapped it out, but that we have a responsibility to document these quirks. In the StimulusReflex project, our first step in solving any problem is to try and ask if it is something that should just be fully explained in the docs.
I am writing docs for AllFutures and would be happy to share anything useful from that work upstream to Active Entity.
I'm getting ActiveModel::UnknownAttributeError
in rails 6.1.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.