Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it works only with ActiveRecord? #2

Closed
okliv opened this issue Aug 3, 2017 · 9 comments
Closed

Is it works only with ActiveRecord? #2

okliv opened this issue Aug 3, 2017 · 9 comments

Comments

@okliv
Copy link

okliv commented Aug 3, 2017

Is it possible to use it with sequel gem, for example?

@rennanoliveira
Copy link

I believe its possible to create a sequel adapter following the pattern at
'lib/trestle/adapters' and setting it on the :default_adapter config option on a project, right?

@okliv
Copy link
Author

okliv commented Aug 3, 2017

sure, if we look at the ActiveRecord adapter
https://github.com/TrestleAdmin/trestle/blob/master/lib/trestle/adapters/active_record_adapter.rb
all methods (except default_attributes) looks pretty trivial to reproduce with sequel related methods...

i can even try to do it but hope for any approvements from contributors before i dedicate some time for this

@spohlenz
Copy link
Member

spohlenz commented Aug 3, 2017

As @rennanoliveira mentioned, creating an adapter will be the correct way to do this. I'd definitely love to have a Sequel adapter included in Trestle core, although I'm not myself particularly familiar with it, so if you'd love to have a go then that'd be great.

If you find any of the adapter abstractions insufficient for Sequel, please let me know and we can try to make the necessary changes.

@okliv
Copy link
Author

okliv commented Aug 4, 2017

Today i have successfully seen my sequel model's data under /admin route so i can confirm it has a potential to be a frontend for sequel. during this quick test i had some errors too... mostly, NoMethodError for my sequel model class (i defined all missing methods, except model_name, "manually" for now just to remove exceptions):

#lib/trestle/resource.rb:155:in `default_model_name'
#fixed with Sequel::Model.plugin :active_model
model_name

#lib/trestle/attribute.rb:30:in `inheritance_column?'
inheritance_column

#lib/trestle/form/fields/check_box.rb:23:in `block (2 levels) in field'
human_attribute_name(attr)

So, i think, this methods need to be extracted to adapter too (or, will it be better to extend sequel class definition?)

In a few days i will try to test more deeply (for example, for now i've defined default_attributes as a simple Attribute.new() collection because reflections mapping for sequel models need more time)

going to test search and auth plugins...

UPD (search-plugin):

search is very basic one (not parametric) so there is no any problems at all (but, honestly, i need much more functional search like ransack-based for ActiveAdmin or https://github.com/djellemah/philtre for sequel)

as a quick workaround for now for me (just because there is only one text field that can be used for search) i've done this:

  #after i added a "philtre" gem  
  search do |q|
    Philtre.new( eval("{#{q}}") ).apply( MySequelModel.dataset )
  end

and now i can simply input into the search field something like this: title_like: "bread" or id: 1234 and filtering works with title LIKE "%bread%" or ID=1234 search snippets according to the philtre syntax. it is not very user friendly (especially, for the end-user), but in my case, something - is better than nothing

auth will try to look tomorrow...

@spohlenz
Copy link
Member

spohlenz commented Aug 6, 2017

It sounds like you're making some good progress @okliv. If it's alright, I'd love to see what your Adapter class looks like so far. Feel free to start a PR if you're so inclined.

  1. For model_name and human_attribute_name, these methods come from ActiveModel and IMO are pretty important for I18n. I'll look into how we can make these methods work even when the model doesn't define them. This would also entail making ActiveModel an explicit dependency (which should not be an issue since Rails is already a dependency).

  2. inheritance_column will almost definitely need to be moved into the Adapter class somehow. The methods inheritance_column and primary_key are used by the Attribute class to determine when to show fields in the automatic tables and forms, so perhaps we should have methods such as show_in_table?(attribute) and show_in_form?(attribute) on Adapter. This would allow adapters to also define additional logic for hidden fields if required.

What about auth and, especially, search plugins? Will it be comparable simple to adapt too, how do you think?

The trestle-search plugin at this stage has a "bring your own search method" approach, so I don't foresee any issues there.

For trestle-auth, I see one instance (https://github.com/TrestleAdmin/trestle-auth/blob/master/lib/trestle/auth/controller_methods.rb#L17) where I need to add an extra configuration block. Otherwise the configuration blocks should provide enough flexibility to bring your own User class with a non-ActiveRecord ORM.

@okliv
Copy link
Author

okliv commented Aug 6, 2017

i think it is little early to make a PR because i didn't test it enough, so here is just a snippet with sequel adapter code
https://gist.github.com/okliv/4822101ec49d16056fc56ad540ad7ca3

full list of changes to get it work is here: https://gist.github.com/okliv/52465d38a116688edf0033985e273d27

and UPD 2 (about auth plugin). i get it work too.

all changes i made for now (on auth plugin side) to get it expectably work are listed here https://gist.github.com/okliv/dbdcbb52076beaa9523ea972ca301203

This was referenced Aug 10, 2017
@spohlenz
Copy link
Member

I've just pushed a heap of commits that should pave the way for completing Sequel adapter support. I still need to finish testing in my Sequel demo app, but it's looking good so far.

@spohlenz
Copy link
Member

Given the latest changes I have made to Trestle, here is my SequelAdapter module so far: https://gist.github.com/spohlenz/0b218de7a1636f5757bd6d30cc1abbb5

@spohlenz
Copy link
Member

I've pushed a SequelAdapter to master (https://github.com/TrestleAdmin/trestle/blob/master/lib/trestle/adapters/sequel_adapter.rb).

It can be used by adding the following line to your trestle.rb:

config.default_adapter = Trestle::Adapters.compose(Trestle::Adapters::SequelAdapter)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants