I am thinking of using dockyard validations in my project
I have options as to how to use the Validations-Mixin from dockyard
Approach 1 to have a BaseController which will mixin this Validations-Mixin and get the functionality in all controllers
Approach 2 use the Mixin directly.
Pros of 1
no need to import ember-validations in every file and mixin manually , and I get the functionality of validations in all the controllers.
Cons of 1
Validations might not be required in all the controllers , may be it's required only in 50% of the cases, also might reorder the way existing controllers add the mixins.
Pros of 2
Simple, only who need the functionality will import and use it.
Cons of 2
importing in each file where required and adding the mixin.
I think you should use that mixin directly. Why?
Because there is no need to extend it in every controller in your app. Extending mixins is a way to obtain shared functionality between controllers that share that functionality. I am pretty sure that not every controller in your app needs validation.
From my experience I can say that only some percent of controllers use validations (it depends on the app, but it's just a rough mean value). Import the mixin where it's needed.
Related
Is there any design pattern in Rails by which i can use helper methods in models, without using include ActionView::Helpers?
ActionView::Helpers are intended to be used in your view code. If you have code that you want to use in you model and view, I would recommend creating a helper method in your model.
If you want to create a method that is used in multiple models, I would use concerns. Concerns are used to extract common chunks of code from models to DRY them up.
Look here for more info
How to use concerns in Rails 4
http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
An alternative to concerns is to create a superclass for common models to inherit from where you can add helper methods
You can for example call one helper at a time if you want.
ApplicationController.helpers.my_helper_method
I'm sorry if this is a basic question, but I have a mixin I want to use in multiple apps.
Is there a best practice on where this mixin should go?
Thanks
You should create a file mixins.py in your utils/ folder and use it your apps.
I would create an app which includes the Mixin and rely on this app in all those multiple apps which should use the Mixin. A short example as i don't know the purpose of your Mixin:
If you want to write a Mixin which is related to authentication i would recommend to create an app call myauth (where my should be some other prefix)- put the Mixin there and all other apps will rely on myauth in this case. myauth might later also include other pieces related to authentication.
If this Mixin will be the only thing you want to reuse in other apps you might just create a non specifiy purpose app like mygeneralpurposelibs and put it there together with stuff necessary for completely different purposes.
I prefer the topic related apps like myauth instead of the lib buckets. As usual the final app structure relies on your specific needs.
In my Django site I use Django REST framework. To not repeat myself, I created a couple mixins to simplify writing my view sets (here is an example of a similar style of mixin).
My question is how do I go about testing such a mixin? If it was a regular view exposed on some URL, I would use APITestCase provided by the framework, but since my mixin is not a standalone object and needs to be inherited from to be actually useful, this way of testing is not applicable.
Is there some way to mock the rest of the view? Or do I have to create a dummy application using this mixin and do integration tests?
You should test Django REST Framework view mixins the same way that you should test standard Django view mixins. You test it when attached to a view or you mock up all of the parts of a standard view that it would rely on.
You should be able to test mixins when attached to views in the same way that you would test standard views, because mixins should behave consistently in ways that are testable. You may need to create test views for each case that the mixin works with, which is what we had to do with DRF JSON API when creating the tests.
In the case of Django REST Framework Bulk, the best way would probably be to create a view that has the mixins attached to it, and test that it responds as expected. For the bulk mixins, lets assume that the following needs to be tested:
Single object POST still works
Multiple object POST on the list
Multiple object PUT on the list
Multiple object PATCH on the list
Multiple object DELETE on the list
This would require at least five tests, most likely split up for what they are testing. You would also need to make sure that the tests cover the hooks that are also being called, as well as any side effects that are expected.
The Python unittest.mock module helps a lot with mocking views (if you want to do that) and allows you to determine if methods (such as the hooks) are called.
I have an existing DetailController and DetailView in my app that has some pretty complicated UI / data manipulation logic (hotkeys, copy paste, duplication, autocomplete, etc) -- the view sends UI events to the controller; the controller handles the logic.
I want to convert this to an Ember component.
Does this basically mean I merge the view and controller into DetailComponent? This seems messy and wrong to me.
If not, how do I use controllers and views internally within a component? That is, I still want the complete isolation and well-defined public interface of the component, but internally within the component, I'd like to use controllers and views for organization. Is that possible?
Is it possible to use {{render}}, {{view}}, {{partial}} within the component template?
Does this basically mean I merge the view and controller into DetailComponent? This seems messy and wrong to me.
Yes that is what it means.
internally within the component, I'd like to use controllers and views for organization. Is that possible?
So component basically replaces a single view/controller pair. Beyond that a component is just an extension of Ember.View and can be organized just like any other view.
Is it possible to use {{render}}, {{view}}, {{partial}} within the component template?
Yes. Any of those helpers will work.
I'm working on an e-commerce framework for Django. The chief design goal is to provide the bare minimum functionality in terms of models and view, instead allowing the users of the library to extend or replace the components with their own.
The reasoning for this is that trying to develop a one-size-fits-all solution to e-commerce leads to overcomplicated code which is often far from optimal.
One approach to tackling this seems to be using inversion-of-control, either through Django's settings file or import hacks, but I've come up against a bit of a problem due to how Django registers its models.
The e-commerce framework provides a bunch of abstract models, as well as concrete versions in {app_label}/models.py. Views make use of Django's get_model(app_label,model) function to return the model class without having to hard-code the reference.
This approach has some problems:
Users have to mimic the structure of the framework's apps, ie the app_label and effectively replace our version of the app with their own
Because of the way the admin site works by looking for admin.py in each installed app, they have to mimic or explicitly import the framework's admin classes in order to use them. But by importing them, the register method gets called so they have to be unregistered if a user wants to customise them.
The user has to be extremely careful about how they import concrete models from the core framework. This is because Django's base model metaclass automatically registers a model with the app cache as soon as the class definition is read (ie upon __new__), and the first model registered with a specific label is the one you're stuck with. So you have to define all your override models BEFORE you import any of the core models. This means you end up with messy situations of having a bunch of imports at the bottom of your modules rather than the top.
My thinking is to go further down the inversion-of-control rabbit hole:
All references to core components (models, views, admin, etc) replaced with calls to an IoC container
For all the core (e-commerce framework) models, replace the use of Django's base model metaclass with one that doesn't automatically register the models, then have the container explicitly register them on startup.
My question:
Is there a better way to solve this problem? The goal is to make it easy to customise the framework and override functionality without having to learn lots of annoying tricks. The key seems to be with models and the admin site.
I appreciate that using an IoC container isn't a common pattern in the Django world, so I want to avoid it if possible, but it is seeming like the right solution.
Did you look at the code from other projects with a similar approach?
Not sure if this way covers your needs, but imo the code of django-shop is worth to look at.
This framework provides the basic logic, allowing you to provide custom logic where needed.
customize via models
eg see the productmodel.py
#==============================================================================
# Extensibility
#==============================================================================
PRODUCT_MODEL = getattr(settings, 'SHOP_PRODUCT_MODEL',
'shop.models.defaults.product.Product')
Product = load_class(PRODUCT_MODEL, 'SHOP_PRODUCT_MODEL')
customize via logic/urls
eg see the shop's simplevariation-plugin
It extends the cart-logic, so it hooks in via urlpattern:
(r'^shop/cart/', include(simplevariations_urls)),
(r'^shop/', include(shop_urls)),
and extends the views:
...
from shop.views.cart import CartDetails
class SimplevariationCartDetails(CartDetails):
"""Cart view that answers GET and POSTS request."""
...
The framework provides several points to hook-in, the simplevariation-plugin mentionned above additionally provides a cart-modifier:
SHOP_CART_MODIFIERS = [
...
'shop_simplevariations.cart_modifier.ProductOptionsModifier',
...
]
I worry that this explanation is not very understandable, it is difficult to briefly summarize this concept. But take a look at the django-shop project and some of its extensions: ecosystem