Laravel Nova Searchable Select In Action - laravel-nova

I have a requirement to create an action to clone some assets to a new organization. I want to give admin user provision to select the assets and then select an organization and run the action to transfer the assets. But we have a long list of assets and also organizations. So the whole hanged up cause it was trying to load all the org list for each asset. Is there any way to have searchable option in Nova action?
$organizations = resolve('App\Repository\OrganizationRepository\OrganizationRepositoryInterface')->getAll()->pluck('company_name', 'id');
return [
Select::make('Organization', 'id')->rules('required', 'exists:organizations,id')->options($organizations)
];

If you're using Nova ^3.0, the Select field comes with the searchable method, so you may use it like this:
return [
Select::make('Organization', 'id')
->searchable()
->rules('required', 'exists:organizations,id')
->options($organizations)
];
The Nova documentation has a example image: https://nova.laravel.com/docs/3.0/resources/fields.html#searchable-select-fields

Related

rails can you reference a model within a model?

I seem to have run into a problem with trying to use a model in another model in Rails.
I am pulling a list of users from Active Directory with LDAP in a dropdown. I want to parse the cn that I get from Ldap into a firstname and lastname.
The problem I am running into is that I need to find a record in the users model. The parsing is being done in observations.rb.
Observation.rb:
def parse_employee
#emp_name = '' #initialize
self.employee_raw = self.employee_raw[2...-2] # get rid of the quotes and brackets
#emp_name = self.employee_raw.split(' ') # split first/last names
#emp_first_name = #emp_name[0] #Grab the first name
#emp_last_name = #emp_name[1] # grab the surname
#user = User.where("last_name like ?", #emp_last_name)
self.employee_id = #user.id
end
I've played with this quite a bit and it appears that I can't reference other models from within a model.
To sum up, what I am trying to do is
1. Have the user select the appropriate person from a dropdown that is pulled via LDAP from active directory.
2. Use the first and last names to find the appropriate user in my user table (Right now I'm just trying to get it to work with the last name as that is unique enough)
3. When I find the correct user in the user table, enter that id in the employee_id field in my observations table.

ActiveAdmin csv export records order

I have ActiveAdmin in my rails app. I was trying to disable default order which ActiveAdmin apply when retrieve records, I've add this to my resource:
config.sort_order = ''
Now when I visit html version of page it properly get the records without order but when I export to csv it retrieves records with order:
SELECT "vw_survey_packs".* FROM "vw_survey_packs" WHERE ("vw_survey_packs"."id" > 22542) ORDER BY "vw_survey_packs"."id" ASC LIMIT 1000
How can I change that?
See this issue on github. Basically: it's not possible without changing the code yourself.

Programmatically generated Spree Taxons not showing tree in Admin

I am trying to generate a Spree Commerce taxonomy programmatically within a Ruby script (a la seeds.rb). In the customer-facing product pages the Taxons appear, but they do not work from the Admin pages.
The "tree" view of the Taxonomy shows the root node, but no children
On the product edit page, I cannot add any of my Taxons
Here's an example:
taxonomy_stones = Spree::Taxonomy.where(:name => 'Gemstone Type').first_or_create
tax_stones = Spree::Taxon.where(name: "Gemstone", parent: nil, taxonomy: taxonomy_stones).first_or_create
tax_diamond = Spree::Taxon.where(name: "Diamond", parent: tax_stones, taxonomy: taxonomy_stones).first_or_create
tax_fancy_yellow = Spree::Taxon.where(name: "Fancy Yellow Diamond", parent: tax_diamond, taxonomy: taxonomy_stones).first_or_create
tax_fancy_pink = Spree::Taxon.where(name: "Fancy Pink Diamond", parent: tax_diamond, taxonomy: taxonomy_stones).first_or_create
When I run this, entries appear in the database for my Taxonomy and Taxons. I am able to programmatically associate Products to the Taxons:
product_BL212.taxons << tax_diamond
I'm guessing that my Taxon-creation code is incomplete or incorrect in some way, but I am not sure how. Can anyone who is more familiar with Spree's internals provide an example of doing this correctly?
Further Observations
I used the Admin UI to create sample Taxonomies and have compared the database entries to my generated ones. The name and permalink fields in spree_taxons are blank for my generated Taxons, but not the Spree-created ones. When I manually populated some of the values for the root node and two sample children, the taxonomy/taxons still don't work correctly in Admin.
Is it important to have these values populated in spree_taxons when spree_taxons_translations has the needed information?
If so, how do I get Spree to populate the values for these fields correctly?
Calling Spree::Taxon.rebuild! after adding the Taxons resolves the issue.
I had a similar issue where I had the code
Spree::Taxonomy.find_or_create_by(name:'Brands')
Spree::Taxon.find_or_initialize_by(name:'brand_name').update(taxonomy: Spree::Taxonomy.find_by(name:'Brands'))
Running Spree::Taxon.rebuild! did not solve the issue. I found out that creating a Taxonomy also creates a Taxon with it and I had to use the ID for the associated taxon in the parent column for the new taxon I was creating.
So
Spree::Taxon.find_or_initialize_by(name:'brand_name').update(taxonomy: Spree::Taxonomy.find_by(name:'Brands'))
had to be changed to
Spree::Taxon.find_or_initialize_by(name:'brand_name').update(taxonomy: Spree::Taxonomy.find_by(name:'Brands'), parent: Spree::Taxon.find_by(name:'Brands'))
The Taxon.parent attribute expects a root object.
For example:
t = Spree::Taxonomy.create(name: "Season")
tt = Spree::Taxon.create(name: "Winter", taxonomy: t, parent: t.root)

Getting total count of FB Likes for a web-app page using Rails

I am building a rails app where there is an image gallery and users will be able to hit the facebook like button for each image they open. Each image can have its seperate page URL. I plan to use the facebook like plugin so that users can like the photos as web-pages. So far I was able to find that if I do https://graph.facebook.com/?id=http://www.mysite.com/ImagePage i get:
{
"id": "http://www.mysite.com/ImagePage",
"shares": 30
}
This leaves me with 2 problems.
The shares property is not exactly the total count of likes, it is only the count of times the link made it to the user's facebook wall. How do I get the number of likes?
Is there an eventHandler or something I can use to know when a user clicks Like? so that I can store that information? I want to store the likes count at my end so that I can show the gallery in the order of descending number of total likes in each day.
I have come across the rails Koala gem but I am not sure if I need to use that for my application yet, as I do not have the need to log in users using facebook login/connect. Please advise if you think I need to do so to do what I mentioned above.
you have to use FQL.
see this example in php, im sure you know how to handle it in ruby:
$fql = 'SELECT url, share_count, like_count, comment_count, total_count
FROM link_stat WHERE url="http://stackoverflow.com"';
$json = file_get_contents('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql));
result looks like:
[
{
"url" : "http://stackoverflow.com",
"share_count" : 1353,
"like_count" : 332,
"comment_count" : 538,
"total_count" : 2223
}
]
For those who don't know how to modify the code seen in the previous answer by yourself, you mostly have two choices:
Use facebook gems available that support running FQL queries (for example: fb_graph)
Sample code:
require 'fb_graph'
array = FbGraph::Query.new(
'SELECT name FROM user WHERE uid = me()'
).fetch(ACCESS_TOKEN)
p array
Use a simple Ruby code that basically just does FQL queries, as seen on this accepted answer (Facebook FQL Query with Ruby)

django admin permissions to modify model's attributes

we are using django to develop a customer-management application, and we need to set permissions to an agent whether he/she can edit the customer's attributes ().
for example,
if i have a model:
class Customer(models.Model):
# basic information
name = models.CharField(max_length=150) # the name of this customer
date = models.DateField(auto_now_add=True) # the date that this customer is created
# personal information
citizen_id = models.BigIntegerField(blank=True)
phone = models.BigIntegerField(max_length=20, blank=True)
work = models.CharField(max_length=100, blank=True)
address = models.CharField(max_length=300, blank=True)
bank_card = models.BigIntegerField()
# installation detail
primary = models.IntegerField(default=0)
secondary = models.IntegerField(default=0)
region = models.ForeignKey(Region) # the region that this customer currently lives in
type = models.ForeignKey(Type) # the installation type
group = models.ForeignKey(Group)
STATUS_CHOICES = (
('Active', 'Active'),
('Inactive', 'Inactive'),
('Transfered', 'Transfered'),
('Closed', 'Closed'),
('Suspended', 'Suspended'),
('Cancelled', 'Cancelled'),
)
status = models.CharField(max_length=40, choices=STATUS_CHOICES)
and I want to be able to set the permissions for editing some of the fields, but the current permission system only allow you add/change/delete a model instance, where the "change" allows an user to edit all the attributes in that model, which is not what we really want.
user A can edit phone, address, work and citizen_id
user B can only edit phone and address,
user C can edit citizen_id, .....
etc...
and I want to be able to set different permissions
Is it possible to make this happen? It'd be really helpful if I could use the django admin system to manage agents and customers.
=======================
thank you so much for FallenAngel's reply.
I think that's exactly what we want.
this is what I've tried,
in admin.py
class CustomerAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, extra_context=None):
agent = Agent.object.get(user=request.user)
permitted_fields = agent.permitted_fields # assume i have this setup...
self.readonly_fields = get_not_permitted_fields(premitted_fields) # assume I have this function written somewhere
return super(CustomerAdmin, self).change_view(request, object_id,
extra_context=None)
this works exactly that way I want: for the not permitted fields, set them to readonly...
thanks again,
That is possible... You must use django admin methods as they described in here... You must define add_view, change_view and changelist_view functions...
You must also find a way to group users (groups might be a good way, or you can use permissions). I create a model that extends User model, but you can find your own way...
Second write basic add, change and listview founctions like:
class CustomerAdmin(admin.ModelAdmin):
list_display= ["fields that will be used for all users"]
def changelist_view(self, request, extra_context=None):
if request.user == "type a":
self.list_display.extend["list of other fields"]
return super(CustomerAdmin, self).changelist_view(request, extra_context)
You must specify add, change (and changelist if required) views and handle each user type. Then Django will show related fields for the user. You extend list_display, fileds, exclude, list_filter and such django admin field display methods...

Resources