This Code Challenge is to show the implementation on a concern within a rails app that multiple models can use in order to store custom attributes without having to do a DB change to add that attribute.
git clone [email protected]:mboya/engie-cc.git
create and setup the DB - this is running SQLite at the moment
rake db:create db:migrate
To check if the app has good/working tests, run:
rails test
to run a specific file test
rails test test/models/customer_test.rb
to run a specific file test block
rails test test/models/customer_test.rb:17
rails console
app has been setup up so that once the above code has been run it will automatically connect to the defined database
>> Customer.create({name: 'customer', phone_number: '555-555-555'})
>> customer = _
>> customer.set_custom_attribute(:email, '[email protected]')
>> customer.save
The above quesry will be able to create a customer and add a custom attribute and value to the records. Other functions can be run against the same record to manipulate and pull data from it
fetch
>> customer = Customer.find 1
>> customer.get_custom_attribute(:email)
remove
>> customer = Customer.find 1
>> customer.remove_custom_attribute(:email)
to view all the keys/custom attributes available against a specific record
>> customer = Custome.find 1
>> customer.custom_attributes_keys?
and also to check if a key exists within a record
>> customer = Customer.find 1
>> customer.custom_attribute?(:email)
and for search, this can work across multiple models that are linked to the concern
>> Customer.find_by_custom_attribute(:email, '[email protected]')